blob: 85c8f835deddff5b8890ab27bacb93eac14cddca [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 Bao5d2e3bd2017-06-23 22:23:50 -070042WearRecoveryUI::WearRecoveryUI()
Tao Baoeea3af32017-08-11 13:50:24 -070043 : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE),
44 kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) {
45 // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked().
Tao Bao0470cee2017-08-02 17:11:04 -070046
47 // TODO: The following three variables are likely not needed. The first two are detected
48 // automatically in ScreenRecoveryUI::LoadAnimation(), based on the actual files seen on device.
Tao Bao5d2e3bd2017-06-23 22:23:50 -070049 intro_frames = 22;
50 loop_frames = 60;
Tao Bao0470cee2017-08-02 17:11:04 -070051
52 touch_screen_allowed_ = true;
Tao Bao337db142015-08-20 14:52:57 -070053
Tao Bao5d2e3bd2017-06-23 22:23:50 -070054 for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL;
Tao Bao337db142015-08-20 14:52:57 -070055}
56
Tao Bao99b2d772017-06-23 22:47:03 -070057int WearRecoveryUI::GetProgressBaseline() const {
Tao Bao0470cee2017-08-02 17:11:04 -070058 return kProgressBarBaseline;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070059}
60
Tao Bao337db142015-08-20 14:52:57 -070061// Draw background frame on the screen. Does not flip pages.
62// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070063// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070064void WearRecoveryUI::draw_background_locked() {
65 pagesIdentical = false;
66 gr_color(0, 0, 0, 255);
67 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -070068
Tao Bao5d2e3bd2017-06-23 22:23:50 -070069 if (currentIcon != NONE) {
70 GRSurface* surface;
71 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
72 if (!intro_done) {
73 surface = introFrames[current_frame];
74 } else {
75 surface = loopFrames[current_frame];
76 }
77 } else {
78 surface = backgroundIcon[currentIcon];
Tao Bao337db142015-08-20 14:52:57 -070079 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -070080
81 int width = gr_get_width(surface);
82 int height = gr_get_height(surface);
83
84 int x = (gr_fb_width() - width) / 2;
85 int y = (gr_fb_height() - height) / 2;
86
87 gr_blit(surface, 0, 0, width, height, x, y);
88 }
Tao Bao337db142015-08-20 14:52:57 -070089}
90
Tao Baoea78d862017-06-28 14:52:17 -070091static const char* SWIPE_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070092 "Swipe up/down to move.",
93 "Swipe left/right to select.",
94 "",
95 NULL
Tao Bao337db142015-08-20 14:52:57 -070096};
97
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070098// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070099void WearRecoveryUI::draw_screen_locked() {
100 char cur_selection_str[50];
Tao Bao337db142015-08-20 14:52:57 -0700101
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700102 draw_background_locked();
103 if (!show_text) {
104 draw_foreground_locked();
105 } else {
106 SetColor(TEXT_FILL);
107 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -0700108
Tao Bao0470cee2017-08-02 17:11:04 -0700109 int y = kMarginHeight;
110 int x = kMarginWidth;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700111 if (show_menu) {
112 std::string recovery_fingerprint =
113 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
114 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700115 y += DrawTextLine(x + 4, y, "Android Recovery", true);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700116 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700117 y += DrawTextLine(x + 4, y, chunk.c_str(), false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700118 }
Tao Bao337db142015-08-20 14:52:57 -0700119
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700120 // This is actually the help strings.
Tao Baoea78d862017-06-28 14:52:17 -0700121 y += DrawTextLines(x + 4, y, SWIPE_HELP);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700122 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700123 y += DrawTextLines(x + 4, y, menu_headers_);
Tao Bao337db142015-08-20 14:52:57 -0700124
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700125 // Show the current menu item number in relation to total number if
126 // items don't fit on the screen.
127 if (menu_items > menu_end - menu_start) {
128 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
129 gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1);
130 y += char_height_ + 4;
131 }
Tao Bao337db142015-08-20 14:52:57 -0700132
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700133 // Menu begins here
134 SetColor(MENU);
Tao Bao337db142015-08-20 14:52:57 -0700135
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700136 for (int i = menu_start; i < menu_end; ++i) {
137 if (i == menu_sel) {
138 // draw the highlight bar
139 SetColor(MENU_SEL_BG);
140 gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2);
141 // white text of selected item
142 SetColor(MENU_SEL_FG);
143 if (menu_[i][0]) {
144 gr_text(gr_sys_font(), x + 4, y, menu_[i], 1);
145 }
146 SetColor(MENU);
147 } else if (menu_[i][0]) {
148 gr_text(gr_sys_font(), x + 4, y, menu_[i], 0);
Tao Bao337db142015-08-20 14:52:57 -0700149 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700150 y += char_height_ + 4;
151 }
152 SetColor(MENU);
153 y += 4;
154 gr_fill(0, y, gr_fb_width(), y + 2);
155 y += 4;
Tao Bao337db142015-08-20 14:52:57 -0700156 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700157
158 SetColor(LOG);
159
160 // display from the bottom up, until we hit the top of the
161 // screen, the bottom of the menu, or we've displayed the
162 // entire text buffer.
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700163 int row = (text_top_ + text_rows_ - 1) % text_rows_;
164 size_t count = 0;
Tao Bao0470cee2017-08-02 17:11:04 -0700165 for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700166 ty -= char_height_, ++count) {
167 gr_text(gr_sys_font(), x + 4, ty, text_[row], 0);
168 --row;
169 if (row < 0) row = text_rows_ - 1;
170 }
171 }
Tao Bao337db142015-08-20 14:52:57 -0700172}
173
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700174// TODO merge drawing routines with screen_ui
175void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700176 draw_screen_locked();
177 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700178}
179
Tao Bao736d59c2017-01-03 10:15:33 -0800180bool WearRecoveryUI::Init(const std::string& locale) {
181 if (!ScreenRecoveryUI::Init(locale)) {
182 return false;
183 }
Tao Bao337db142015-08-20 14:52:57 -0700184
Tao Bao736d59c2017-01-03 10:15:33 -0800185 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
186 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
Tim Kryger825b6b02016-11-29 13:33:29 -0800187
Tao Bao736d59c2017-01-03 10:15:33 -0800188 // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING]
189 // as NULL which is fine since draw_background_locked() doesn't use them.
Tim Kryger825b6b02016-11-29 13:33:29 -0800190
Tao Bao736d59c2017-01-03 10:15:33 -0800191 return true;
Prashant Malanif7f9e502016-03-10 03:40:20 +0000192}
193
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700194void WearRecoveryUI::SetStage(int current, int max) {
195}
Tao Bao337db142015-08-20 14:52:57 -0700196
Tao Bao736d59c2017-01-03 10:15:33 -0800197void WearRecoveryUI::Print(const char* fmt, ...) {
198 char buf[256];
199 va_list ap;
200 va_start(ap, fmt);
201 vsnprintf(buf, 256, fmt, ap);
202 va_end(ap);
Tao Bao337db142015-08-20 14:52:57 -0700203
Tao Bao736d59c2017-01-03 10:15:33 -0800204 fputs(buf, stdout);
Tao Bao337db142015-08-20 14:52:57 -0700205
Tao Bao736d59c2017-01-03 10:15:33 -0800206 // This can get called before ui_init(), so be careful.
207 pthread_mutex_lock(&updateMutex);
208 if (text_rows_ > 0 && text_cols_ > 0) {
209 char* ptr;
210 for (ptr = buf; *ptr != '\0'; ++ptr) {
211 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700212 text_[text_row_][text_col_] = '\0';
Tao Bao736d59c2017-01-03 10:15:33 -0800213 text_col_ = 0;
214 text_row_ = (text_row_ + 1) % text_rows_;
215 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
216 }
217 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700218 }
Tao Bao736d59c2017-01-03 10:15:33 -0800219 text_[text_row_][text_col_] = '\0';
220 update_screen_locked();
221 }
222 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700223}
224
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700225void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700226 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700227 pthread_mutex_lock(&updateMutex);
228 if (text_rows_ > 0 && text_cols_ > 0) {
229 menu_headers_ = headers;
230 size_t i = 0;
231 // "i < text_rows_" is removed from the loop termination condition,
232 // which is different from the one in ScreenRecoveryUI::StartMenu().
233 // Because WearRecoveryUI supports scrollable menu, it's fine to have
234 // more entries than text_rows_. The menu may be truncated otherwise.
235 // Bug: 23752519
236 for (; items[i] != nullptr; i++) {
237 strncpy(menu_[i], items[i], text_cols_ - 1);
238 menu_[i][text_cols_ - 1] = '\0';
Tao Bao337db142015-08-20 14:52:57 -0700239 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700240 menu_items = i;
241 show_menu = true;
242 menu_sel = initial_selection;
243 menu_start = 0;
Tao Bao1e27d142017-08-26 08:32:29 -0700244 menu_end = text_rows_ - 1 - kMenuUnusableRows;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700245 if (menu_items <= menu_end) menu_end = menu_items;
246 update_screen_locked();
247 }
248 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700249}
250
251int WearRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700252 int old_sel;
253 pthread_mutex_lock(&updateMutex);
254 if (show_menu) {
255 old_sel = menu_sel;
256 menu_sel = sel;
257 if (menu_sel < 0) menu_sel = 0;
258 if (menu_sel >= menu_items) menu_sel = menu_items - 1;
259 if (menu_sel < menu_start) {
260 menu_start--;
261 menu_end--;
262 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
263 menu_end++;
264 menu_start++;
Tao Bao337db142015-08-20 14:52:57 -0700265 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700266 sel = menu_sel;
267 if (menu_sel != old_sel) update_screen_locked();
268 }
269 pthread_mutex_unlock(&updateMutex);
270 return sel;
Tao Bao337db142015-08-20 14:52:57 -0700271}
272
Tao Bao337db142015-08-20 14:52:57 -0700273void WearRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700274 std::vector<off_t> offsets;
275 offsets.push_back(ftello(fp));
276 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700277
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700278 struct stat sb;
279 fstat(fileno(fp), &sb);
Tao Bao337db142015-08-20 14:52:57 -0700280
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700281 bool show_prompt = false;
282 while (true) {
283 if (show_prompt) {
284 Print("--(%d%% of %d bytes)--",
285 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
286 static_cast<int>(sb.st_size));
287 Redraw();
288 while (show_prompt) {
289 show_prompt = false;
290 int key = WaitKey();
291 if (key == KEY_POWER || key == KEY_ENTER) {
292 return;
293 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
294 if (offsets.size() <= 1) {
Tao Bao337db142015-08-20 14:52:57 -0700295 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700296 } else {
297 offsets.pop_back();
298 fseek(fp, offsets.back(), SEEK_SET);
299 }
Tao Bao337db142015-08-20 14:52:57 -0700300 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700301 if (feof(fp)) {
302 return;
303 }
304 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700305 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700306 }
307 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700308 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700309
310 int ch = getc(fp);
311 if (ch == EOF) {
312 text_row_ = text_top_ = text_rows_ - 2;
313 show_prompt = true;
314 } else {
315 PutChar(ch);
316 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
317 text_top_ = text_row_;
318 show_prompt = true;
319 }
320 }
321 }
Tao Bao337db142015-08-20 14:52:57 -0700322}
323
324void WearRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700325 pthread_mutex_lock(&updateMutex);
326 if (ch != '\n') text_[text_row_][text_col_++] = ch;
327 if (ch == '\n' || text_col_ >= text_cols_) {
328 text_col_ = 0;
329 ++text_row_;
330 }
331 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700332}
333
334void WearRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700335 FILE* fp = fopen_path(filename, "re");
336 if (fp == nullptr) {
337 Print(" Unable to open %s: %s\n", filename, strerror(errno));
338 return;
339 }
340 ShowFile(fp);
341 fclose(fp);
Tao Bao337db142015-08-20 14:52:57 -0700342}
343
Prashant Malani0eb41c32016-02-25 18:27:03 -0800344void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700345 va_list ap;
346 va_start(ap, fmt);
347 PrintV(fmt, false, ap);
348 va_end(ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800349}
350
351void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700352 std::string str;
353 android::base::StringAppendV(&str, fmt, ap);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800354
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700355 if (copy_to_stdout) {
356 fputs(str.c_str(), stdout);
357 }
Prashant Malani0eb41c32016-02-25 18:27:03 -0800358
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700359 pthread_mutex_lock(&updateMutex);
360 if (text_rows_ > 0 && text_cols_ > 0) {
361 for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) {
362 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700363 text_[text_row_][text_col_] = '\0';
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700364 text_col_ = 0;
365 text_row_ = (text_row_ + 1) % text_rows_;
366 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
367 }
368 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Prashant Malani0eb41c32016-02-25 18:27:03 -0800369 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700370 text_[text_row_][text_col_] = '\0';
371 update_screen_locked();
372 }
373 pthread_mutex_unlock(&updateMutex);
Prashant Malani0eb41c32016-02-25 18:27:03 -0800374}