blob: 71a535199700afbd83b381032dfd6f1bf6f9da52 [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
Tao Bao0ecbd762017-01-16 21:16:58 -080033#include <android-base/properties.h>
34#include <android-base/strings.h>
35#include <android-base/stringprintf.h>
36#include <minui/minui.h>
37
Tao Bao337db142015-08-20 14:52:57 -070038#include "common.h"
39#include "device.h"
Tao Bao337db142015-08-20 14:52:57 -070040
Tao Bao337db142015-08-20 14:52:57 -070041// There's only (at most) one of these objects, and global callbacks
42// (for pthread_create, and the input event system) need to find it,
43// so use a global variable.
44static WearRecoveryUI* self = NULL;
45
46// Return the current time as a double (including fractions of a second).
47static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070048 struct timeval tv;
49 gettimeofday(&tv, NULL);
50 return tv.tv_sec + tv.tv_usec / 1000000.0;
Tao Bao337db142015-08-20 14:52:57 -070051}
52
Tao Bao5d2e3bd2017-06-23 22:23:50 -070053WearRecoveryUI::WearRecoveryUI()
Tao Baoeea3af32017-08-11 13:50:24 -070054 : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE),
55 kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) {
56 // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked().
Tao Bao0470cee2017-08-02 17:11:04 -070057
58 // TODO: The following three variables are likely not needed. The first two are detected
59 // automatically in ScreenRecoveryUI::LoadAnimation(), based on the actual files seen on device.
Tao Bao5d2e3bd2017-06-23 22:23:50 -070060 intro_frames = 22;
61 loop_frames = 60;
Tao Bao0470cee2017-08-02 17:11:04 -070062
63 touch_screen_allowed_ = true;
Tao Bao337db142015-08-20 14:52:57 -070064
Tao Bao5d2e3bd2017-06-23 22:23:50 -070065 for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL;
Tao Bao337db142015-08-20 14:52:57 -070066
Tao Bao5d2e3bd2017-06-23 22:23:50 -070067 self = this;
Tao Bao337db142015-08-20 14:52:57 -070068}
69
Tao Bao99b2d772017-06-23 22:47:03 -070070int WearRecoveryUI::GetProgressBaseline() const {
Tao Bao0470cee2017-08-02 17:11:04 -070071 return kProgressBarBaseline;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070072}
73
Tao Bao337db142015-08-20 14:52:57 -070074// Draw background frame on the screen. Does not flip pages.
75// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070076// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070077void WearRecoveryUI::draw_background_locked() {
78 pagesIdentical = false;
79 gr_color(0, 0, 0, 255);
80 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -070081
Tao Bao5d2e3bd2017-06-23 22:23:50 -070082 if (currentIcon != NONE) {
83 GRSurface* surface;
84 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
85 if (!intro_done) {
86 surface = introFrames[current_frame];
87 } else {
88 surface = loopFrames[current_frame];
89 }
90 } else {
91 surface = backgroundIcon[currentIcon];
Tao Bao337db142015-08-20 14:52:57 -070092 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070093
94 int width = gr_get_width(surface);
95 int height = gr_get_height(surface);
96
97 int x = (gr_fb_width() - width) / 2;
98 int y = (gr_fb_height() - height) / 2;
99
100 gr_blit(surface, 0, 0, width, height, x, y);
101 }
Tao Bao337db142015-08-20 14:52:57 -0700102}
103
Tao Baoea78d862017-06-28 14:52:17 -0700104static const char* SWIPE_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700105 "Swipe up/down to move.",
106 "Swipe left/right to select.",
107 "",
108 NULL
Tao Bao337db142015-08-20 14:52:57 -0700109};
110
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700111// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700112void WearRecoveryUI::draw_screen_locked() {
113 char cur_selection_str[50];
Tao Bao337db142015-08-20 14:52:57 -0700114
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700115 draw_background_locked();
116 if (!show_text) {
117 draw_foreground_locked();
118 } else {
119 SetColor(TEXT_FILL);
120 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -0700121
Tao Bao0470cee2017-08-02 17:11:04 -0700122 int y = kMarginHeight;
123 int x = kMarginWidth;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700124 if (show_menu) {
125 std::string recovery_fingerprint =
126 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
127 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700128 y += DrawTextLine(x + 4, y, "Android Recovery", true);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700129 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700130 y += DrawTextLine(x + 4, y, chunk.c_str(), false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700131 }
Tao Bao337db142015-08-20 14:52:57 -0700132
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700133 // This is actually the help strings.
Tao Baoea78d862017-06-28 14:52:17 -0700134 y += DrawTextLines(x + 4, y, SWIPE_HELP);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700135 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700136 y += DrawTextLines(x + 4, y, menu_headers_);
Tao Bao337db142015-08-20 14:52:57 -0700137
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700138 // Show the current menu item number in relation to total number if
139 // items don't fit on the screen.
140 if (menu_items > menu_end - menu_start) {
141 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
142 gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1);
143 y += char_height_ + 4;
144 }
Tao Bao337db142015-08-20 14:52:57 -0700145
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700146 // Menu begins here
147 SetColor(MENU);
Tao Bao337db142015-08-20 14:52:57 -0700148
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700149 for (int i = menu_start; i < menu_end; ++i) {
150 if (i == menu_sel) {
151 // draw the highlight bar
152 SetColor(MENU_SEL_BG);
153 gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2);
154 // white text of selected item
155 SetColor(MENU_SEL_FG);
156 if (menu_[i][0]) {
157 gr_text(gr_sys_font(), x + 4, y, menu_[i], 1);
158 }
159 SetColor(MENU);
160 } 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 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700163 y += char_height_ + 4;
164 }
165 SetColor(MENU);
166 y += 4;
167 gr_fill(0, y, gr_fb_width(), y + 2);
168 y += 4;
Tao Bao337db142015-08-20 14:52:57 -0700169 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700170
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;
177 int row = (text_top_ + text_rows_ - 1) % text_rows_;
178 size_t count = 0;
Tao Bao0470cee2017-08-02 17:11:04 -0700179 for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700180 ty -= char_height_, ++count) {
181 gr_text(gr_sys_font(), x + 4, ty, text_[row], 0);
182 --row;
183 if (row < 0) row = text_rows_ - 1;
184 }
185 }
Tao Bao337db142015-08-20 14:52:57 -0700186}
187
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700188// TODO merge drawing routines with screen_ui
189void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700190 draw_screen_locked();
191 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700192}
193
Tao Bao736d59c2017-01-03 10:15:33 -0800194bool WearRecoveryUI::Init(const std::string& locale) {
195 if (!ScreenRecoveryUI::Init(locale)) {
196 return false;
197 }
Tao Bao337db142015-08-20 14:52:57 -0700198
Tao Bao736d59c2017-01-03 10:15:33 -0800199 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
200 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
Tim Kryger825b6b02016-11-29 13:33:29 -0800201
Tao Bao736d59c2017-01-03 10:15:33 -0800202 // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING]
203 // as NULL which is fine since draw_background_locked() doesn't use them.
Tim Kryger825b6b02016-11-29 13:33:29 -0800204
Tao Bao736d59c2017-01-03 10:15:33 -0800205 return true;
Prashant Malanif7f9e502016-03-10 03:40:20 +0000206}
207
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700208void WearRecoveryUI::SetStage(int current, int max) {
209}
Tao Bao337db142015-08-20 14:52:57 -0700210
Tao Bao736d59c2017-01-03 10:15:33 -0800211void WearRecoveryUI::Print(const char* fmt, ...) {
212 char buf[256];
213 va_list ap;
214 va_start(ap, fmt);
215 vsnprintf(buf, 256, fmt, ap);
216 va_end(ap);
Tao Bao337db142015-08-20 14:52:57 -0700217
Tao Bao736d59c2017-01-03 10:15:33 -0800218 fputs(buf, stdout);
Tao Bao337db142015-08-20 14:52:57 -0700219
Tao Bao736d59c2017-01-03 10:15:33 -0800220 // This can get called before ui_init(), so be careful.
221 pthread_mutex_lock(&updateMutex);
222 if (text_rows_ > 0 && text_cols_ > 0) {
223 char* ptr;
224 for (ptr = buf; *ptr != '\0'; ++ptr) {
225 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700226 text_[text_row_][text_col_] = '\0';
Tao Bao736d59c2017-01-03 10:15:33 -0800227 text_col_ = 0;
228 text_row_ = (text_row_ + 1) % text_rows_;
229 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
230 }
231 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700232 }
Tao Bao736d59c2017-01-03 10:15:33 -0800233 text_[text_row_][text_col_] = '\0';
234 update_screen_locked();
235 }
236 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700237}
238
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700239void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700240 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700241 pthread_mutex_lock(&updateMutex);
242 if (text_rows_ > 0 && text_cols_ > 0) {
243 menu_headers_ = headers;
244 size_t i = 0;
245 // "i < text_rows_" is removed from the loop termination condition,
246 // which is different from the one in ScreenRecoveryUI::StartMenu().
247 // Because WearRecoveryUI supports scrollable menu, it's fine to have
248 // more entries than text_rows_. The menu may be truncated otherwise.
249 // Bug: 23752519
250 for (; items[i] != nullptr; i++) {
251 strncpy(menu_[i], items[i], text_cols_ - 1);
252 menu_[i][text_cols_ - 1] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700253 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700254 menu_items = i;
255 show_menu = true;
256 menu_sel = initial_selection;
257 menu_start = 0;
Tao Bao1e27d142017-08-26 08:32:29 -0700258 menu_end = text_rows_ - 1 - kMenuUnusableRows;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700259 if (menu_items <= menu_end) menu_end = menu_items;
260 update_screen_locked();
261 }
262 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700263}
264
265int WearRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700266 int old_sel;
267 pthread_mutex_lock(&updateMutex);
268 if (show_menu) {
269 old_sel = menu_sel;
270 menu_sel = sel;
271 if (menu_sel < 0) menu_sel = 0;
272 if (menu_sel >= menu_items) menu_sel = menu_items - 1;
273 if (menu_sel < menu_start) {
274 menu_start--;
275 menu_end--;
276 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
277 menu_end++;
278 menu_start++;
Tao Bao337db142015-08-20 14:52:57 -0700279 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700280 sel = menu_sel;
281 if (menu_sel != old_sel) update_screen_locked();
282 }
283 pthread_mutex_unlock(&updateMutex);
284 return sel;
Tao Bao337db142015-08-20 14:52:57 -0700285}
286
Tao Bao337db142015-08-20 14:52:57 -0700287void WearRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700288 std::vector<off_t> offsets;
289 offsets.push_back(ftello(fp));
290 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700291
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700292 struct stat sb;
293 fstat(fileno(fp), &sb);
Tao Bao337db142015-08-20 14:52:57 -0700294
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700295 bool show_prompt = false;
296 while (true) {
297 if (show_prompt) {
298 Print("--(%d%% of %d bytes)--",
299 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
300 static_cast<int>(sb.st_size));
301 Redraw();
302 while (show_prompt) {
303 show_prompt = false;
304 int key = WaitKey();
305 if (key == KEY_POWER || key == KEY_ENTER) {
306 return;
307 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
308 if (offsets.size() <= 1) {
Tao Bao337db142015-08-20 14:52:57 -0700309 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700310 } else {
311 offsets.pop_back();
312 fseek(fp, offsets.back(), SEEK_SET);
313 }
Tao Bao337db142015-08-20 14:52:57 -0700314 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700315 if (feof(fp)) {
316 return;
317 }
318 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700319 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700320 }
321 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700322 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700323
324 int ch = getc(fp);
325 if (ch == EOF) {
326 text_row_ = text_top_ = text_rows_ - 2;
327 show_prompt = true;
328 } else {
329 PutChar(ch);
330 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
331 text_top_ = text_row_;
332 show_prompt = true;
333 }
334 }
335 }
Tao Bao337db142015-08-20 14:52:57 -0700336}
337
338void WearRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700339 pthread_mutex_lock(&updateMutex);
340 if (ch != '\n') text_[text_row_][text_col_++] = ch;
341 if (ch == '\n' || text_col_ >= text_cols_) {
342 text_col_ = 0;
343 ++text_row_;
344 }
345 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700346}
347
348void WearRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700349 FILE* fp = fopen_path(filename, "re");
350 if (fp == nullptr) {
351 Print(" Unable to open %s: %s\n", filename, strerror(errno));
352 return;
353 }
354 ShowFile(fp);
355 fclose(fp);
Tao Bao337db142015-08-20 14:52:57 -0700356}
357
Prashant Malani0eb41c32016-02-25 18:27:03 -0800358void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700359 va_list ap;
360 va_start(ap, fmt);
361 PrintV(fmt, false, ap);
362 va_end(ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800363}
364
365void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700366 std::string str;
367 android::base::StringAppendV(&str, fmt, ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800368
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700369 if (copy_to_stdout) {
370 fputs(str.c_str(), stdout);
371 }
Prashant Malani0eb41c32016-02-25 18:27:03 -0800372
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700373 pthread_mutex_lock(&updateMutex);
374 if (text_rows_ > 0 && text_cols_ > 0) {
375 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
376 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700377 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700378 text_col_ = 0;
379 text_row_ = (text_row_ + 1) % text_rows_;
380 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
381 }
382 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800383 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700384 text_[text_row_][text_col_] = '\0';
385 update_screen_locked();
386 }
387 pthread_mutex_unlock(&updateMutex);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800388}