blob: b8801a0b387085c2c2df959a48c5c322c07c880b [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 Bao0470cee2017-08-02 17:11:04 -070054 : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), menu_unusable_rows(9) {
55 // TODO: menu_unusable_rows should be computed based on the lines in draw_screen_locked().
56
57 // TODO: The following three variables are likely not needed. The first two are detected
58 // automatically in ScreenRecoveryUI::LoadAnimation(), based on the actual files seen on device.
Tao Bao5d2e3bd2017-06-23 22:23:50 -070059 intro_frames = 22;
60 loop_frames = 60;
Tao Bao0470cee2017-08-02 17:11:04 -070061
62 touch_screen_allowed_ = true;
Tao Bao337db142015-08-20 14:52:57 -070063
Tao Bao5d2e3bd2017-06-23 22:23:50 -070064 for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL;
Tao Bao337db142015-08-20 14:52:57 -070065
Tao Bao5d2e3bd2017-06-23 22:23:50 -070066 self = this;
Tao Bao337db142015-08-20 14:52:57 -070067}
68
Tao Bao99b2d772017-06-23 22:47:03 -070069int WearRecoveryUI::GetProgressBaseline() const {
Tao Bao0470cee2017-08-02 17:11:04 -070070 return kProgressBarBaseline;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070071}
72
Tao Bao337db142015-08-20 14:52:57 -070073// Draw background frame on the screen. Does not flip pages.
74// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070075// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070076void WearRecoveryUI::draw_background_locked() {
77 pagesIdentical = false;
78 gr_color(0, 0, 0, 255);
79 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -070080
Tao Bao5d2e3bd2017-06-23 22:23:50 -070081 if (currentIcon != NONE) {
82 GRSurface* surface;
83 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
84 if (!intro_done) {
85 surface = introFrames[current_frame];
86 } else {
87 surface = loopFrames[current_frame];
88 }
89 } else {
90 surface = backgroundIcon[currentIcon];
Tao Bao337db142015-08-20 14:52:57 -070091 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070092
93 int width = gr_get_width(surface);
94 int height = gr_get_height(surface);
95
96 int x = (gr_fb_width() - width) / 2;
97 int y = (gr_fb_height() - height) / 2;
98
99 gr_blit(surface, 0, 0, width, height, x, y);
100 }
Tao Bao337db142015-08-20 14:52:57 -0700101}
102
Tao Baoea78d862017-06-28 14:52:17 -0700103static const char* SWIPE_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700104 "Swipe up/down to move.",
105 "Swipe left/right to select.",
106 "",
107 NULL
Tao Bao337db142015-08-20 14:52:57 -0700108};
109
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700110// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700111void WearRecoveryUI::draw_screen_locked() {
112 char cur_selection_str[50];
Tao Bao337db142015-08-20 14:52:57 -0700113
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700114 draw_background_locked();
115 if (!show_text) {
116 draw_foreground_locked();
117 } else {
118 SetColor(TEXT_FILL);
119 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -0700120
Tao Bao0470cee2017-08-02 17:11:04 -0700121 int y = kMarginHeight;
122 int x = kMarginWidth;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700123 if (show_menu) {
124 std::string recovery_fingerprint =
125 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
126 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700127 y += DrawTextLine(x + 4, y, "Android Recovery", true);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700128 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700129 y += DrawTextLine(x + 4, y, chunk.c_str(), false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700130 }
Tao Bao337db142015-08-20 14:52:57 -0700131
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700132 // This is actually the help strings.
Tao Baoea78d862017-06-28 14:52:17 -0700133 y += DrawTextLines(x + 4, y, SWIPE_HELP);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700134 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700135 y += DrawTextLines(x + 4, y, menu_headers_);
Tao Bao337db142015-08-20 14:52:57 -0700136
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700137 // 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);
141 gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1);
142 y += char_height_ + 4;
143 }
Tao Bao337db142015-08-20 14:52:57 -0700144
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700145 // Menu begins here
146 SetColor(MENU);
Tao Bao337db142015-08-20 14:52:57 -0700147
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700148 for (int i = menu_start; i < menu_end; ++i) {
149 if (i == menu_sel) {
150 // draw the highlight bar
151 SetColor(MENU_SEL_BG);
152 gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2);
153 // white text of selected item
154 SetColor(MENU_SEL_FG);
155 if (menu_[i][0]) {
156 gr_text(gr_sys_font(), x + 4, y, menu_[i], 1);
157 }
158 SetColor(MENU);
159 } else if (menu_[i][0]) {
160 gr_text(gr_sys_font(), x + 4, y, menu_[i], 0);
Tao Bao337db142015-08-20 14:52:57 -0700161 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700162 y += char_height_ + 4;
163 }
164 SetColor(MENU);
165 y += 4;
166 gr_fill(0, y, gr_fb_width(), y + 2);
167 y += 4;
Tao Bao337db142015-08-20 14:52:57 -0700168 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700169
170 SetColor(LOG);
171
172 // display from the bottom up, until we hit the top of the
173 // screen, the bottom of the menu, or we've displayed the
174 // entire text buffer.
175 int ty;
176 int row = (text_top_ + text_rows_ - 1) % text_rows_;
177 size_t count = 0;
Tao Bao0470cee2017-08-02 17:11:04 -0700178 for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700179 ty -= char_height_, ++count) {
180 gr_text(gr_sys_font(), x + 4, ty, text_[row], 0);
181 --row;
182 if (row < 0) row = text_rows_ - 1;
183 }
184 }
Tao Bao337db142015-08-20 14:52:57 -0700185}
186
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700187// TODO merge drawing routines with screen_ui
188void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700189 draw_screen_locked();
190 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700191}
192
Sen Jiangd5304492016-12-09 16:20:49 -0800193bool WearRecoveryUI::InitTextParams() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700194 if (!ScreenRecoveryUI::InitTextParams()) {
195 return false;
196 }
Tao Bao337db142015-08-20 14:52:57 -0700197
Tao Bao0470cee2017-08-02 17:11:04 -0700198 text_cols_ = (gr_fb_width() - (kMarginWidth * 2)) / char_width_;
Tao Bao337db142015-08-20 14:52:57 -0700199
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700200 if (text_rows_ > kMaxRows) text_rows_ = kMaxRows;
201 if (text_cols_ > kMaxCols) text_cols_ = kMaxCols;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700202
Tao Bao0470cee2017-08-02 17:11:04 -0700203 visible_text_rows = (gr_fb_height() - (kMarginHeight * 2)) / char_height_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700204 return true;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700205}
Tao Bao337db142015-08-20 14:52:57 -0700206
Tao Bao736d59c2017-01-03 10:15:33 -0800207bool WearRecoveryUI::Init(const std::string& locale) {
208 if (!ScreenRecoveryUI::Init(locale)) {
209 return false;
210 }
Tao Bao337db142015-08-20 14:52:57 -0700211
Tao Bao736d59c2017-01-03 10:15:33 -0800212 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
213 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
Tim Kryger825b6b02016-11-29 13:33:29 -0800214
Tao Bao736d59c2017-01-03 10:15:33 -0800215 // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING]
216 // as NULL which is fine since draw_background_locked() doesn't use them.
Tim Kryger825b6b02016-11-29 13:33:29 -0800217
Tao Bao736d59c2017-01-03 10:15:33 -0800218 return true;
Prashant Malanif7f9e502016-03-10 03:40:20 +0000219}
220
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700221void WearRecoveryUI::SetStage(int current, int max) {
222}
Tao Bao337db142015-08-20 14:52:57 -0700223
Tao Bao736d59c2017-01-03 10:15:33 -0800224void WearRecoveryUI::Print(const char* fmt, ...) {
225 char buf[256];
226 va_list ap;
227 va_start(ap, fmt);
228 vsnprintf(buf, 256, fmt, ap);
229 va_end(ap);
Tao Bao337db142015-08-20 14:52:57 -0700230
Tao Bao736d59c2017-01-03 10:15:33 -0800231 fputs(buf, stdout);
Tao Bao337db142015-08-20 14:52:57 -0700232
Tao Bao736d59c2017-01-03 10:15:33 -0800233 // This can get called before ui_init(), so be careful.
234 pthread_mutex_lock(&updateMutex);
235 if (text_rows_ > 0 && text_cols_ > 0) {
236 char* ptr;
237 for (ptr = buf; *ptr != '\0'; ++ptr) {
238 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700239 text_[text_row_][text_col_] = '\0';
Tao Bao736d59c2017-01-03 10:15:33 -0800240 text_col_ = 0;
241 text_row_ = (text_row_ + 1) % text_rows_;
242 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
243 }
244 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700245 }
Tao Bao736d59c2017-01-03 10:15:33 -0800246 text_[text_row_][text_col_] = '\0';
247 update_screen_locked();
248 }
249 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700250}
251
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700252void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700253 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700254 pthread_mutex_lock(&updateMutex);
255 if (text_rows_ > 0 && text_cols_ > 0) {
256 menu_headers_ = headers;
257 size_t i = 0;
258 // "i < text_rows_" is removed from the loop termination condition,
259 // which is different from the one in ScreenRecoveryUI::StartMenu().
260 // Because WearRecoveryUI supports scrollable menu, it's fine to have
261 // more entries than text_rows_. The menu may be truncated otherwise.
262 // Bug: 23752519
263 for (; items[i] != nullptr; i++) {
264 strncpy(menu_[i], items[i], text_cols_ - 1);
265 menu_[i][text_cols_ - 1] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700266 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700267 menu_items = i;
268 show_menu = true;
269 menu_sel = initial_selection;
270 menu_start = 0;
271 menu_end = visible_text_rows - 1 - menu_unusable_rows;
272 if (menu_items <= menu_end) menu_end = menu_items;
273 update_screen_locked();
274 }
275 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700276}
277
278int WearRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700279 int old_sel;
280 pthread_mutex_lock(&updateMutex);
281 if (show_menu) {
282 old_sel = menu_sel;
283 menu_sel = sel;
284 if (menu_sel < 0) menu_sel = 0;
285 if (menu_sel >= menu_items) menu_sel = menu_items - 1;
286 if (menu_sel < menu_start) {
287 menu_start--;
288 menu_end--;
289 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
290 menu_end++;
291 menu_start++;
Tao Bao337db142015-08-20 14:52:57 -0700292 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700293 sel = menu_sel;
294 if (menu_sel != old_sel) update_screen_locked();
295 }
296 pthread_mutex_unlock(&updateMutex);
297 return sel;
Tao Bao337db142015-08-20 14:52:57 -0700298}
299
Tao Bao337db142015-08-20 14:52:57 -0700300void WearRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700301 std::vector<off_t> offsets;
302 offsets.push_back(ftello(fp));
303 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700304
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700305 struct stat sb;
306 fstat(fileno(fp), &sb);
Tao Bao337db142015-08-20 14:52:57 -0700307
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700308 bool show_prompt = false;
309 while (true) {
310 if (show_prompt) {
311 Print("--(%d%% of %d bytes)--",
312 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
313 static_cast<int>(sb.st_size));
314 Redraw();
315 while (show_prompt) {
316 show_prompt = false;
317 int key = WaitKey();
318 if (key == KEY_POWER || key == KEY_ENTER) {
319 return;
320 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
321 if (offsets.size() <= 1) {
Tao Bao337db142015-08-20 14:52:57 -0700322 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700323 } else {
324 offsets.pop_back();
325 fseek(fp, offsets.back(), SEEK_SET);
326 }
Tao Bao337db142015-08-20 14:52:57 -0700327 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700328 if (feof(fp)) {
329 return;
330 }
331 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700332 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700333 }
334 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700335 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700336
337 int ch = getc(fp);
338 if (ch == EOF) {
339 text_row_ = text_top_ = text_rows_ - 2;
340 show_prompt = true;
341 } else {
342 PutChar(ch);
343 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
344 text_top_ = text_row_;
345 show_prompt = true;
346 }
347 }
348 }
Tao Bao337db142015-08-20 14:52:57 -0700349}
350
351void WearRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700352 pthread_mutex_lock(&updateMutex);
353 if (ch != '\n') text_[text_row_][text_col_++] = ch;
354 if (ch == '\n' || text_col_ >= text_cols_) {
355 text_col_ = 0;
356 ++text_row_;
357 }
358 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700359}
360
361void WearRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700362 FILE* fp = fopen_path(filename, "re");
363 if (fp == nullptr) {
364 Print(" Unable to open %s: %s\n", filename, strerror(errno));
365 return;
366 }
367 ShowFile(fp);
368 fclose(fp);
Tao Bao337db142015-08-20 14:52:57 -0700369}
370
Prashant Malani0eb41c32016-02-25 18:27:03 -0800371void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700372 va_list ap;
373 va_start(ap, fmt);
374 PrintV(fmt, false, ap);
375 va_end(ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800376}
377
378void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700379 std::string str;
380 android::base::StringAppendV(&str, fmt, ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800381
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700382 if (copy_to_stdout) {
383 fputs(str.c_str(), stdout);
384 }
Prashant Malani0eb41c32016-02-25 18:27:03 -0800385
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700386 pthread_mutex_lock(&updateMutex);
387 if (text_rows_ > 0 && text_cols_ > 0) {
388 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
389 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700390 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700391 text_col_ = 0;
392 text_row_ = (text_row_ + 1) % text_rows_;
393 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
394 }
395 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800396 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700397 text_[text_row_][text_col_] = '\0';
398 update_screen_locked();
399 }
400 pthread_mutex_unlock(&updateMutex);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800401}