blob: e4806718d914b17165b082b54ad6ca07c8b62a7a [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 Baocff82692017-08-26 07:56:44 -070021#include <pthread.h>
Tao Bao337db142015-08-20 14:52:57 -070022#include <stdarg.h>
Tao Bao337db142015-08-20 14:52:57 -070023#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
Tao Bao736d59c2017-01-03 10:15:33 -080031#include <string>
Tao Bao337db142015-08-20 14:52:57 -070032#include <vector>
33
Tao Bao0ecbd762017-01-16 21:16:58 -080034#include <android-base/properties.h>
35#include <android-base/strings.h>
36#include <android-base/stringprintf.h>
37#include <minui/minui.h>
38
Tao Bao337db142015-08-20 14:52:57 -070039#include "common.h"
40#include "device.h"
Tao Bao337db142015-08-20 14:52:57 -070041
Tao Bao337db142015-08-20 14:52:57 -070042// Return the current time as a double (including fractions of a second).
43static double now() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070044 struct timeval tv;
45 gettimeofday(&tv, NULL);
46 return tv.tv_sec + tv.tv_usec / 1000000.0;
Tao Bao337db142015-08-20 14:52:57 -070047}
48
Tao Bao5d2e3bd2017-06-23 22:23:50 -070049WearRecoveryUI::WearRecoveryUI()
Tao Baoeea3af32017-08-11 13:50:24 -070050 : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE),
51 kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) {
52 // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked().
Tao Bao0470cee2017-08-02 17:11:04 -070053
54 // TODO: The following three variables are likely not needed. The first two are detected
55 // automatically in ScreenRecoveryUI::LoadAnimation(), based on the actual files seen on device.
Tao Bao5d2e3bd2017-06-23 22:23:50 -070056 intro_frames = 22;
57 loop_frames = 60;
Tao Bao0470cee2017-08-02 17:11:04 -070058
59 touch_screen_allowed_ = true;
Tao Bao337db142015-08-20 14:52:57 -070060
Tao Bao5d2e3bd2017-06-23 22:23:50 -070061 for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL;
Tao Bao337db142015-08-20 14:52:57 -070062}
63
Tao Bao99b2d772017-06-23 22:47:03 -070064int WearRecoveryUI::GetProgressBaseline() const {
Tao Bao0470cee2017-08-02 17:11:04 -070065 return kProgressBarBaseline;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070066}
67
Tao Bao337db142015-08-20 14:52:57 -070068// Draw background frame on the screen. Does not flip pages.
69// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070070// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070071void WearRecoveryUI::draw_background_locked() {
72 pagesIdentical = false;
73 gr_color(0, 0, 0, 255);
74 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -070075
Tao Bao5d2e3bd2017-06-23 22:23:50 -070076 if (currentIcon != NONE) {
77 GRSurface* surface;
78 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
79 if (!intro_done) {
80 surface = introFrames[current_frame];
81 } else {
82 surface = loopFrames[current_frame];
83 }
84 } else {
85 surface = backgroundIcon[currentIcon];
Tao Bao337db142015-08-20 14:52:57 -070086 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070087
88 int width = gr_get_width(surface);
89 int height = gr_get_height(surface);
90
91 int x = (gr_fb_width() - width) / 2;
92 int y = (gr_fb_height() - height) / 2;
93
94 gr_blit(surface, 0, 0, width, height, x, y);
95 }
Tao Bao337db142015-08-20 14:52:57 -070096}
97
Tao Baoea78d862017-06-28 14:52:17 -070098static const char* SWIPE_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070099 "Swipe up/down to move.",
100 "Swipe left/right to select.",
101 "",
102 NULL
Tao Bao337db142015-08-20 14:52:57 -0700103};
104
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700105// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700106void WearRecoveryUI::draw_screen_locked() {
107 char cur_selection_str[50];
Tao Bao337db142015-08-20 14:52:57 -0700108
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700109 draw_background_locked();
110 if (!show_text) {
111 draw_foreground_locked();
112 } else {
113 SetColor(TEXT_FILL);
114 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -0700115
Tao Bao0470cee2017-08-02 17:11:04 -0700116 int y = kMarginHeight;
117 int x = kMarginWidth;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700118 if (show_menu) {
119 std::string recovery_fingerprint =
120 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
121 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700122 y += DrawTextLine(x + 4, y, "Android Recovery", true);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700123 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700124 y += DrawTextLine(x + 4, y, chunk.c_str(), false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700125 }
Tao Bao337db142015-08-20 14:52:57 -0700126
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700127 // This is actually the help strings.
Tao Baoea78d862017-06-28 14:52:17 -0700128 y += DrawTextLines(x + 4, y, SWIPE_HELP);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700129 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700130 y += DrawTextLines(x + 4, y, menu_headers_);
Tao Bao337db142015-08-20 14:52:57 -0700131
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700132 // Show the current menu item number in relation to total number if
133 // items don't fit on the screen.
134 if (menu_items > menu_end - menu_start) {
135 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
136 gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1);
137 y += char_height_ + 4;
138 }
Tao Bao337db142015-08-20 14:52:57 -0700139
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700140 // Menu begins here
141 SetColor(MENU);
Tao Bao337db142015-08-20 14:52:57 -0700142
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700143 for (int i = menu_start; i < menu_end; ++i) {
144 if (i == menu_sel) {
145 // draw the highlight bar
146 SetColor(MENU_SEL_BG);
147 gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2);
148 // white text of selected item
149 SetColor(MENU_SEL_FG);
150 if (menu_[i][0]) {
151 gr_text(gr_sys_font(), x + 4, y, menu_[i], 1);
152 }
153 SetColor(MENU);
154 } else if (menu_[i][0]) {
155 gr_text(gr_sys_font(), x + 4, y, menu_[i], 0);
Tao Bao337db142015-08-20 14:52:57 -0700156 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700157 y += char_height_ + 4;
158 }
159 SetColor(MENU);
160 y += 4;
161 gr_fill(0, y, gr_fb_width(), y + 2);
162 y += 4;
Tao Bao337db142015-08-20 14:52:57 -0700163 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700164
165 SetColor(LOG);
166
167 // display from the bottom up, until we hit the top of the
168 // screen, the bottom of the menu, or we've displayed the
169 // entire text buffer.
170 int ty;
171 int row = (text_top_ + text_rows_ - 1) % text_rows_;
172 size_t count = 0;
Tao Bao0470cee2017-08-02 17:11:04 -0700173 for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700174 ty -= char_height_, ++count) {
175 gr_text(gr_sys_font(), x + 4, ty, text_[row], 0);
176 --row;
177 if (row < 0) row = text_rows_ - 1;
178 }
179 }
Tao Bao337db142015-08-20 14:52:57 -0700180}
181
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700182// TODO merge drawing routines with screen_ui
183void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700184 draw_screen_locked();
185 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700186}
187
Tao Bao736d59c2017-01-03 10:15:33 -0800188bool WearRecoveryUI::Init(const std::string& locale) {
189 if (!ScreenRecoveryUI::Init(locale)) {
190 return false;
191 }
Tao Bao337db142015-08-20 14:52:57 -0700192
Tao Bao736d59c2017-01-03 10:15:33 -0800193 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
194 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
Tim Kryger825b6b02016-11-29 13:33:29 -0800195
Tao Bao736d59c2017-01-03 10:15:33 -0800196 // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING]
197 // as NULL which is fine since draw_background_locked() doesn't use them.
Tim Kryger825b6b02016-11-29 13:33:29 -0800198
Tao Bao736d59c2017-01-03 10:15:33 -0800199 return true;
Prashant Malanif7f9e502016-03-10 03:40:20 +0000200}
201
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700202void WearRecoveryUI::SetStage(int current, int max) {
203}
Tao Bao337db142015-08-20 14:52:57 -0700204
Tao Bao736d59c2017-01-03 10:15:33 -0800205void WearRecoveryUI::Print(const char* fmt, ...) {
206 char buf[256];
207 va_list ap;
208 va_start(ap, fmt);
209 vsnprintf(buf, 256, fmt, ap);
210 va_end(ap);
Tao Bao337db142015-08-20 14:52:57 -0700211
Tao Bao736d59c2017-01-03 10:15:33 -0800212 fputs(buf, stdout);
Tao Bao337db142015-08-20 14:52:57 -0700213
Tao Bao736d59c2017-01-03 10:15:33 -0800214 // This can get called before ui_init(), so be careful.
215 pthread_mutex_lock(&updateMutex);
216 if (text_rows_ > 0 && text_cols_ > 0) {
217 char* ptr;
218 for (ptr = buf; *ptr != '\0'; ++ptr) {
219 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700220 text_[text_row_][text_col_] = '\0';
Tao Bao736d59c2017-01-03 10:15:33 -0800221 text_col_ = 0;
222 text_row_ = (text_row_ + 1) % text_rows_;
223 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
224 }
225 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700226 }
Tao Bao736d59c2017-01-03 10:15:33 -0800227 text_[text_row_][text_col_] = '\0';
228 update_screen_locked();
229 }
230 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700231}
232
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700233void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700234 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700235 pthread_mutex_lock(&updateMutex);
236 if (text_rows_ > 0 && text_cols_ > 0) {
237 menu_headers_ = headers;
238 size_t i = 0;
239 // "i < text_rows_" is removed from the loop termination condition,
240 // which is different from the one in ScreenRecoveryUI::StartMenu().
241 // Because WearRecoveryUI supports scrollable menu, it's fine to have
242 // more entries than text_rows_. The menu may be truncated otherwise.
243 // Bug: 23752519
244 for (; items[i] != nullptr; i++) {
245 strncpy(menu_[i], items[i], text_cols_ - 1);
246 menu_[i][text_cols_ - 1] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700247 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700248 menu_items = i;
249 show_menu = true;
250 menu_sel = initial_selection;
251 menu_start = 0;
Tao Bao1e27d142017-08-26 08:32:29 -0700252 menu_end = text_rows_ - 1 - kMenuUnusableRows;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700253 if (menu_items <= menu_end) menu_end = menu_items;
254 update_screen_locked();
255 }
256 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700257}
258
259int WearRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700260 int old_sel;
261 pthread_mutex_lock(&updateMutex);
262 if (show_menu) {
263 old_sel = menu_sel;
264 menu_sel = sel;
265 if (menu_sel < 0) menu_sel = 0;
266 if (menu_sel >= menu_items) menu_sel = menu_items - 1;
267 if (menu_sel < menu_start) {
268 menu_start--;
269 menu_end--;
270 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
271 menu_end++;
272 menu_start++;
Tao Bao337db142015-08-20 14:52:57 -0700273 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700274 sel = menu_sel;
275 if (menu_sel != old_sel) update_screen_locked();
276 }
277 pthread_mutex_unlock(&updateMutex);
278 return sel;
Tao Bao337db142015-08-20 14:52:57 -0700279}
280
Tao Bao337db142015-08-20 14:52:57 -0700281void WearRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700282 std::vector<off_t> offsets;
283 offsets.push_back(ftello(fp));
284 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700285
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700286 struct stat sb;
287 fstat(fileno(fp), &sb);
Tao Bao337db142015-08-20 14:52:57 -0700288
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700289 bool show_prompt = false;
290 while (true) {
291 if (show_prompt) {
292 Print("--(%d%% of %d bytes)--",
293 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
294 static_cast<int>(sb.st_size));
295 Redraw();
296 while (show_prompt) {
297 show_prompt = false;
298 int key = WaitKey();
299 if (key == KEY_POWER || key == KEY_ENTER) {
300 return;
301 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
302 if (offsets.size() <= 1) {
Tao Bao337db142015-08-20 14:52:57 -0700303 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700304 } else {
305 offsets.pop_back();
306 fseek(fp, offsets.back(), SEEK_SET);
307 }
Tao Bao337db142015-08-20 14:52:57 -0700308 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700309 if (feof(fp)) {
310 return;
311 }
312 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700313 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700314 }
315 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700316 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700317
318 int ch = getc(fp);
319 if (ch == EOF) {
320 text_row_ = text_top_ = text_rows_ - 2;
321 show_prompt = true;
322 } else {
323 PutChar(ch);
324 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
325 text_top_ = text_row_;
326 show_prompt = true;
327 }
328 }
329 }
Tao Bao337db142015-08-20 14:52:57 -0700330}
331
332void WearRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700333 pthread_mutex_lock(&updateMutex);
334 if (ch != '\n') text_[text_row_][text_col_++] = ch;
335 if (ch == '\n' || text_col_ >= text_cols_) {
336 text_col_ = 0;
337 ++text_row_;
338 }
339 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700340}
341
342void WearRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700343 FILE* fp = fopen_path(filename, "re");
344 if (fp == nullptr) {
345 Print(" Unable to open %s: %s\n", filename, strerror(errno));
346 return;
347 }
348 ShowFile(fp);
349 fclose(fp);
Tao Bao337db142015-08-20 14:52:57 -0700350}
351
Prashant Malani0eb41c32016-02-25 18:27:03 -0800352void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700353 va_list ap;
354 va_start(ap, fmt);
355 PrintV(fmt, false, ap);
356 va_end(ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800357}
358
359void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700360 std::string str;
361 android::base::StringAppendV(&str, fmt, ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800362
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700363 if (copy_to_stdout) {
364 fputs(str.c_str(), stdout);
365 }
Prashant Malani0eb41c32016-02-25 18:27:03 -0800366
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700367 pthread_mutex_lock(&updateMutex);
368 if (text_rows_ > 0 && text_cols_ > 0) {
369 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
370 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700371 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700372 text_col_ = 0;
373 text_row_ = (text_row_ + 1) % text_rows_;
374 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
375 }
376 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800377 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700378 text_[text_row_][text_col_] = '\0';
379 update_screen_locked();
380 }
381 pthread_mutex_unlock(&updateMutex);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800382}