blob: b4c63a5ae93fc9a5d2fd8cd793f43b3693295282 [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
Tao Bao736d59c2017-01-03 10:15:33 -080017#include "wear_ui.h"
18
Tao Bao337db142015-08-20 14:52:57 -070019#include <errno.h>
20#include <fcntl.h>
Tao Bao337db142015-08-20 14:52:57 -070021#include <stdarg.h>
Tao Bao337db142015-08-20 14:52:57 -070022#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
Tao Bao736d59c2017-01-03 10:15:33 -080030#include <string>
Tao Bao337db142015-08-20 14:52:57 -070031#include <vector>
32
33#include "common.h"
34#include "device.h"
Elliott Hughescb220402016-09-23 15:30:55 -070035#include "android-base/properties.h"
Elliott Hughes4b166f02015-12-04 15:30:20 -080036#include "android-base/strings.h"
Prashant Malani0eb41c32016-02-25 18:27:03 -080037#include "android-base/stringprintf.h"
Tao Bao337db142015-08-20 14:52:57 -070038
Tao Bao337db142015-08-20 14:52:57 -070039// There's only (at most) one of these objects, and global callbacks
40// (for pthread_create, and the input event system) need to find it,
41// so use a global variable.
42static WearRecoveryUI* self = NULL;
43
44// Return the current time as a double (including fractions of a second).
45static double now() {
46 struct timeval tv;
47 gettimeofday(&tv, NULL);
48 return tv.tv_sec + tv.tv_usec / 1000000.0;
49}
50
51WearRecoveryUI::WearRecoveryUI() :
Tao Bao337db142015-08-20 14:52:57 -070052 progress_bar_y(259),
53 outer_height(0),
54 outer_width(0),
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070055 menu_unusable_rows(0) {
56 intro_frames = 22;
57 loop_frames = 60;
58 animation_fps = 30;
Tao Bao337db142015-08-20 14:52:57 -070059
60 for (size_t i = 0; i < 5; i++)
61 backgroundIcon[i] = NULL;
62
Tao Bao337db142015-08-20 14:52:57 -070063 self = this;
64}
65
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070066int WearRecoveryUI::GetProgressBaseline() {
67 return progress_bar_y;
68}
69
Tao Bao337db142015-08-20 14:52:57 -070070// Draw background frame on the screen. Does not flip pages.
71// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070072// TODO merge drawing routines with screen_ui
73void WearRecoveryUI::draw_background_locked()
Tao Bao337db142015-08-20 14:52:57 -070074{
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070075 pagesIdentical = false;
Tao Bao337db142015-08-20 14:52:57 -070076 gr_color(0, 0, 0, 255);
77 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
78
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070079 if (currentIcon != NONE) {
Tao Bao337db142015-08-20 14:52:57 -070080 GRSurface* surface;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070081 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Tao Bao337db142015-08-20 14:52:57 -070082 if (!intro_done) {
83 surface = introFrames[current_frame];
84 } else {
85 surface = loopFrames[current_frame];
86 }
87 }
88 else {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070089 surface = backgroundIcon[currentIcon];
Tao Bao337db142015-08-20 14:52:57 -070090 }
91
92 int width = gr_get_width(surface);
93 int height = gr_get_height(surface);
94
95 int x = (gr_fb_width() - width) / 2;
96 int y = (gr_fb_height() - height) / 2;
97
98 gr_blit(surface, 0, 0, width, height, x, y);
99 }
100}
101
Tao Bao337db142015-08-20 14:52:57 -0700102static const char* HEADERS[] = {
103 "Swipe up/down to move.",
104 "Swipe left/right to select.",
105 "",
106 NULL
107};
108
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700109// TODO merge drawing routines with screen_ui
Tao Bao337db142015-08-20 14:52:57 -0700110void WearRecoveryUI::draw_screen_locked()
111{
Tao Bao337db142015-08-20 14:52:57 -0700112 char cur_selection_str[50];
113
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700114 draw_background_locked();
115 if (!show_text) {
116 draw_foreground_locked();
117 } else {
Tao Bao337db142015-08-20 14:52:57 -0700118 SetColor(TEXT_FILL);
119 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
120
121 int y = outer_height;
122 int x = outer_width;
123 if (show_menu) {
Elliott Hughescb220402016-09-23 15:30:55 -0700124 std::string recovery_fingerprint =
125 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
Tao Bao337db142015-08-20 14:52:57 -0700126 SetColor(HEADER);
127 DrawTextLine(x + 4, &y, "Android Recovery", true);
128 for (auto& chunk: android::base::Split(recovery_fingerprint, ":")) {
129 DrawTextLine(x +4, &y, chunk.c_str(), false);
130 }
131
132 // This is actually the help strings.
133 DrawTextLines(x + 4, &y, HEADERS);
134 SetColor(HEADER);
135 DrawTextLines(x + 4, &y, menu_headers_);
136
137 // Show the current menu item number in relation to total number if
138 // items don't fit on the screen.
139 if (menu_items > menu_end - menu_start) {
140 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
Damien Bargiacchi35fff612016-08-11 15:57:03 -0700141 gr_text(gr_sys_font(), x+4, y, cur_selection_str, 1);
Prashant Malani7a491222016-03-11 10:00:55 -0800142 y += char_height_+4;
Tao Bao337db142015-08-20 14:52:57 -0700143 }
144
145 // Menu begins here
146 SetColor(MENU);
147
148 for (int i = menu_start; i < menu_end; ++i) {
149
150 if (i == menu_sel) {
151 // draw the highlight bar
152 SetColor(MENU_SEL_BG);
Prashant Malani7a491222016-03-11 10:00:55 -0800153 gr_fill(x, y-2, gr_fb_width()-x, y+char_height_+2);
Tao Bao337db142015-08-20 14:52:57 -0700154 // white text of selected item
155 SetColor(MENU_SEL_FG);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700156 if (menu_[i][0]) {
157 gr_text(gr_sys_font(), x + 4, y, menu_[i], 1);
158 }
Tao Bao337db142015-08-20 14:52:57 -0700159 SetColor(MENU);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700160 } else if (menu_[i][0]) {
161 gr_text(gr_sys_font(), x + 4, y, menu_[i], 0);
Tao Bao337db142015-08-20 14:52:57 -0700162 }
Prashant Malani7a491222016-03-11 10:00:55 -0800163 y += char_height_+4;
Tao Bao337db142015-08-20 14:52:57 -0700164 }
165 SetColor(MENU);
166 y += 4;
167 gr_fill(0, y, gr_fb_width(), y+2);
168 y += 4;
169 }
170
171 SetColor(LOG);
172
173 // display from the bottom up, until we hit the top of the
174 // screen, the bottom of the menu, or we've displayed the
175 // entire text buffer.
176 int ty;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700177 int row = (text_top_ + text_rows_ - 1) % text_rows_;
Tao Bao337db142015-08-20 14:52:57 -0700178 size_t count = 0;
Prashant Malani7a491222016-03-11 10:00:55 -0800179 for (int ty = gr_fb_height() - char_height_ - outer_height;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700180 ty > y + 2 && count < text_rows_;
Prashant Malani7a491222016-03-11 10:00:55 -0800181 ty -= char_height_, ++count) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700182 gr_text(gr_sys_font(), x+4, ty, text_[row], 0);
Tao Bao337db142015-08-20 14:52:57 -0700183 --row;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700184 if (row < 0) row = text_rows_ - 1;
Tao Bao337db142015-08-20 14:52:57 -0700185 }
186 }
187}
188
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700189// TODO merge drawing routines with screen_ui
190void WearRecoveryUI::update_progress_locked() {
191 draw_screen_locked();
192 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700193}
194
Sen Jiangd5304492016-12-09 16:20:49 -0800195bool WearRecoveryUI::InitTextParams() {
196 if (!ScreenRecoveryUI::InitTextParams()) {
197 return false;
198 }
Tao Bao337db142015-08-20 14:52:57 -0700199
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700200 text_cols_ = (gr_fb_width() - (outer_width * 2)) / char_width_;
Tao Bao337db142015-08-20 14:52:57 -0700201
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700202 if (text_rows_ > kMaxRows) text_rows_ = kMaxRows;
203 if (text_cols_ > kMaxCols) text_cols_ = kMaxCols;
204
Prashant Malani7a491222016-03-11 10:00:55 -0800205 visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height_;
Sen Jiangd5304492016-12-09 16:20:49 -0800206 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700207}
Tao Bao337db142015-08-20 14:52:57 -0700208
Tao Bao736d59c2017-01-03 10:15:33 -0800209bool WearRecoveryUI::Init(const std::string& locale) {
210 if (!ScreenRecoveryUI::Init(locale)) {
211 return false;
212 }
Tao Bao337db142015-08-20 14:52:57 -0700213
Tao Bao736d59c2017-01-03 10:15:33 -0800214 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
215 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
Tim Kryger825b6b02016-11-29 13:33:29 -0800216
Tao Bao736d59c2017-01-03 10:15:33 -0800217 // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING]
218 // as NULL which is fine since draw_background_locked() doesn't use them.
Tim Kryger825b6b02016-11-29 13:33:29 -0800219
Tao Bao736d59c2017-01-03 10:15:33 -0800220 return true;
Prashant Malanif7f9e502016-03-10 03:40:20 +0000221}
222
Tao Bao736d59c2017-01-03 10:15:33 -0800223void WearRecoveryUI::SetStage(int current, int max) {}
Tao Bao337db142015-08-20 14:52:57 -0700224
Tao Bao736d59c2017-01-03 10:15:33 -0800225void WearRecoveryUI::Print(const char* fmt, ...) {
226 char buf[256];
227 va_list ap;
228 va_start(ap, fmt);
229 vsnprintf(buf, 256, fmt, ap);
230 va_end(ap);
Tao Bao337db142015-08-20 14:52:57 -0700231
Tao Bao736d59c2017-01-03 10:15:33 -0800232 fputs(buf, stdout);
Tao Bao337db142015-08-20 14:52:57 -0700233
Tao Bao736d59c2017-01-03 10:15:33 -0800234 // This can get called before ui_init(), so be careful.
235 pthread_mutex_lock(&updateMutex);
236 if (text_rows_ > 0 && text_cols_ > 0) {
237 char* ptr;
238 for (ptr = buf; *ptr != '\0'; ++ptr) {
239 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700240 text_[text_row_][text_col_] = '\0';
Tao Bao736d59c2017-01-03 10:15:33 -0800241 text_col_ = 0;
242 text_row_ = (text_row_ + 1) % text_rows_;
243 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
244 }
245 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700246 }
Tao Bao736d59c2017-01-03 10:15:33 -0800247 text_[text_row_][text_col_] = '\0';
248 update_screen_locked();
249 }
250 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700251}
252
253void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700254 int initial_selection) {
Tao Bao337db142015-08-20 14:52:57 -0700255 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700256 if (text_rows_ > 0 && text_cols_ > 0) {
Tao Bao337db142015-08-20 14:52:57 -0700257 menu_headers_ = headers;
258 size_t i = 0;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700259 // "i < text_rows_" is removed from the loop termination condition,
Tao Bao8e9c6802015-09-02 11:20:30 -0700260 // which is different from the one in ScreenRecoveryUI::StartMenu().
261 // Because WearRecoveryUI supports scrollable menu, it's fine to have
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700262 // more entries than text_rows_. The menu may be truncated otherwise.
Tao Bao8e9c6802015-09-02 11:20:30 -0700263 // Bug: 23752519
264 for (; items[i] != nullptr; i++) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700265 strncpy(menu_[i], items[i], text_cols_ - 1);
266 menu_[i][text_cols_ - 1] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700267 }
268 menu_items = i;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700269 show_menu = true;
Tao Bao337db142015-08-20 14:52:57 -0700270 menu_sel = initial_selection;
271 menu_start = 0;
272 menu_end = visible_text_rows - 1 - menu_unusable_rows;
273 if (menu_items <= menu_end)
274 menu_end = menu_items;
275 update_screen_locked();
276 }
277 pthread_mutex_unlock(&updateMutex);
278}
279
280int WearRecoveryUI::SelectMenu(int sel) {
281 int old_sel;
282 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700283 if (show_menu) {
Tao Bao337db142015-08-20 14:52:57 -0700284 old_sel = menu_sel;
285 menu_sel = sel;
286 if (menu_sel < 0) menu_sel = 0;
287 if (menu_sel >= menu_items) menu_sel = menu_items-1;
288 if (menu_sel < menu_start) {
289 menu_start--;
290 menu_end--;
291 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
292 menu_end++;
293 menu_start++;
294 }
295 sel = menu_sel;
296 if (menu_sel != old_sel) update_screen_locked();
297 }
298 pthread_mutex_unlock(&updateMutex);
299 return sel;
300}
301
Tao Bao337db142015-08-20 14:52:57 -0700302void WearRecoveryUI::ShowFile(FILE* fp) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700303 std::vector<off_t> offsets;
304 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700305 ClearText();
306
307 struct stat sb;
308 fstat(fileno(fp), &sb);
309
310 bool show_prompt = false;
311 while (true) {
312 if (show_prompt) {
313 Print("--(%d%% of %d bytes)--",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700314 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
Tao Bao337db142015-08-20 14:52:57 -0700315 static_cast<int>(sb.st_size));
316 Redraw();
317 while (show_prompt) {
318 show_prompt = false;
319 int key = WaitKey();
320 if (key == KEY_POWER || key == KEY_ENTER) {
321 return;
322 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
323 if (offsets.size() <= 1) {
324 show_prompt = true;
325 } else {
326 offsets.pop_back();
327 fseek(fp, offsets.back(), SEEK_SET);
328 }
329 } else {
330 if (feof(fp)) {
331 return;
332 }
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700333 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700334 }
335 }
336 ClearText();
337 }
338
339 int ch = getc(fp);
340 if (ch == EOF) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700341 text_row_ = text_top_ = text_rows_ - 2;
Tao Bao337db142015-08-20 14:52:57 -0700342 show_prompt = true;
343 } else {
344 PutChar(ch);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700345 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
346 text_top_ = text_row_;
Tao Bao337db142015-08-20 14:52:57 -0700347 show_prompt = true;
348 }
349 }
350 }
351}
352
353void WearRecoveryUI::PutChar(char ch) {
354 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700355 if (ch != '\n') text_[text_row_][text_col_++] = ch;
356 if (ch == '\n' || text_col_ >= text_cols_) {
357 text_col_ = 0;
358 ++text_row_;
Tao Bao337db142015-08-20 14:52:57 -0700359 }
360 pthread_mutex_unlock(&updateMutex);
361}
362
363void WearRecoveryUI::ShowFile(const char* filename) {
364 FILE* fp = fopen_path(filename, "re");
365 if (fp == nullptr) {
366 Print(" Unable to open %s: %s\n", filename, strerror(errno));
367 return;
368 }
369 ShowFile(fp);
370 fclose(fp);
371}
372
373void WearRecoveryUI::ClearText() {
374 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700375 text_col_ = 0;
376 text_row_ = 0;
377 text_top_ = 1;
378 for (size_t i = 0; i < text_rows_; ++i) {
379 memset(text_[i], 0, text_cols_ + 1);
Tao Bao337db142015-08-20 14:52:57 -0700380 }
381 pthread_mutex_unlock(&updateMutex);
382}
Prashant Malani0eb41c32016-02-25 18:27:03 -0800383
384void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
385 va_list ap;
386 va_start(ap, fmt);
387 PrintV(fmt, false, ap);
388 va_end(ap);
389}
390
391void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
392 std::string str;
393 android::base::StringAppendV(&str, fmt, ap);
394
395 if (copy_to_stdout) {
396 fputs(str.c_str(), stdout);
397 }
398
399 pthread_mutex_lock(&updateMutex);
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700400 if (text_rows_ > 0 && text_cols_ > 0) {
Prashant Malani0eb41c32016-02-25 18:27:03 -0800401 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700402 if (*ptr == '\n' || text_col_ >= text_cols_) {
403 text_[text_row_][text_col_] = '\0';
404 text_col_ = 0;
405 text_row_ = (text_row_ + 1) % text_rows_;
406 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800407 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700408 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800409 }
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700410 text_[text_row_][text_col_] = '\0';
Prashant Malani0eb41c32016-02-25 18:27:03 -0800411 update_screen_locked();
412 }
413 pthread_mutex_unlock(&updateMutex);
414}