blob: edc39cfb4417ad3a32230d512d9646584e25fef5 [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>
Tao Bao0ecbd762017-01-16 21:16:58 -080035#include <android-base/stringprintf.h>
Tao Baoee8a96a2017-09-01 11:37:50 -070036#include <android-base/strings.h>
Tao Bao0ecbd762017-01-16 21:16:58 -080037#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}
54
Tao Bao99b2d772017-06-23 22:47:03 -070055int WearRecoveryUI::GetProgressBaseline() const {
Tao Bao0470cee2017-08-02 17:11:04 -070056 return kProgressBarBaseline;
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070057}
58
Tao Bao337db142015-08-20 14:52:57 -070059// Draw background frame on the screen. Does not flip pages.
60// Should only be called with updateMutex locked.
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070061// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070062void WearRecoveryUI::draw_background_locked() {
63 pagesIdentical = false;
64 gr_color(0, 0, 0, 255);
65 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -070066
Tao Bao5d2e3bd2017-06-23 22:23:50 -070067 if (currentIcon != NONE) {
Tao Bao79127102017-08-30 15:23:34 -070068 GRSurface* frame = GetCurrentFrame();
69 int frame_width = gr_get_width(frame);
70 int frame_height = gr_get_height(frame);
71 int frame_x = (gr_fb_width() - frame_width) / 2;
72 int frame_y = (gr_fb_height() - frame_height) / 2;
73 gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
Tao Bao5d2e3bd2017-06-23 22:23:50 -070074 }
Tao Bao337db142015-08-20 14:52:57 -070075}
76
Tao Baoea78d862017-06-28 14:52:17 -070077static const char* SWIPE_HELP[] = {
Tao Bao5d2e3bd2017-06-23 22:23:50 -070078 "Swipe up/down to move.",
79 "Swipe left/right to select.",
80 "",
81 NULL
Tao Bao337db142015-08-20 14:52:57 -070082};
83
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -070084// TODO merge drawing routines with screen_ui
Tao Bao5d2e3bd2017-06-23 22:23:50 -070085void WearRecoveryUI::draw_screen_locked() {
86 char cur_selection_str[50];
Tao Bao337db142015-08-20 14:52:57 -070087
Tao Bao5d2e3bd2017-06-23 22:23:50 -070088 draw_background_locked();
89 if (!show_text) {
90 draw_foreground_locked();
91 } else {
92 SetColor(TEXT_FILL);
93 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
Tao Bao337db142015-08-20 14:52:57 -070094
Tao Bao0470cee2017-08-02 17:11:04 -070095 int y = kMarginHeight;
96 int x = kMarginWidth;
Tao Bao5d2e3bd2017-06-23 22:23:50 -070097 if (show_menu) {
98 std::string recovery_fingerprint =
99 android::base::GetProperty("ro.bootimage.build.fingerprint", "");
100 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700101 y += DrawTextLine(x + 4, y, "Android Recovery", true);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700102 for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) {
Tao Baoea78d862017-06-28 14:52:17 -0700103 y += DrawTextLine(x + 4, y, chunk.c_str(), false);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700104 }
Tao Bao337db142015-08-20 14:52:57 -0700105
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700106 // This is actually the help strings.
Tao Baoea78d862017-06-28 14:52:17 -0700107 y += DrawTextLines(x + 4, y, SWIPE_HELP);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700108 SetColor(HEADER);
Tao Baoea78d862017-06-28 14:52:17 -0700109 y += DrawTextLines(x + 4, y, menu_headers_);
Tao Bao337db142015-08-20 14:52:57 -0700110
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700111 // Show the current menu item number in relation to total number if
112 // items don't fit on the screen.
113 if (menu_items > menu_end - menu_start) {
114 sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
115 gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1);
116 y += char_height_ + 4;
117 }
Tao Bao337db142015-08-20 14:52:57 -0700118
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700119 // Menu begins here
120 SetColor(MENU);
Tao Bao337db142015-08-20 14:52:57 -0700121
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700122 for (int i = menu_start; i < menu_end; ++i) {
123 if (i == menu_sel) {
124 // draw the highlight bar
125 SetColor(MENU_SEL_BG);
126 gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2);
127 // white text of selected item
128 SetColor(MENU_SEL_FG);
129 if (menu_[i][0]) {
Tao Baoe15d7a52017-09-07 13:38:51 -0700130 gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1);
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700131 }
132 SetColor(MENU);
133 } else if (menu_[i][0]) {
Tao Baoe15d7a52017-09-07 13:38:51 -0700134 gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0);
Tao Bao337db142015-08-20 14:52:57 -0700135 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700136 y += char_height_ + 4;
137 }
138 SetColor(MENU);
139 y += 4;
140 gr_fill(0, y, gr_fb_width(), y + 2);
141 y += 4;
Tao Bao337db142015-08-20 14:52:57 -0700142 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700143
144 SetColor(LOG);
145
146 // display from the bottom up, until we hit the top of the
147 // screen, the bottom of the menu, or we've displayed the
148 // entire text buffer.
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700149 int row = (text_top_ + text_rows_ - 1) % text_rows_;
150 size_t count = 0;
Tao Bao0470cee2017-08-02 17:11:04 -0700151 for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700152 ty -= char_height_, ++count) {
153 gr_text(gr_sys_font(), x + 4, ty, text_[row], 0);
154 --row;
155 if (row < 0) row = text_rows_ - 1;
156 }
157 }
Tao Bao337db142015-08-20 14:52:57 -0700158}
159
Damien Bargiacchiad8b5a62016-09-09 08:18:06 -0700160// TODO merge drawing routines with screen_ui
161void WearRecoveryUI::update_progress_locked() {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700162 draw_screen_locked();
163 gr_flip();
Tao Bao337db142015-08-20 14:52:57 -0700164}
165
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700166void WearRecoveryUI::SetStage(int current, int max) {
167}
Tao Bao337db142015-08-20 14:52:57 -0700168
Tao Bao736d59c2017-01-03 10:15:33 -0800169void WearRecoveryUI::Print(const char* fmt, ...) {
170 char buf[256];
171 va_list ap;
172 va_start(ap, fmt);
173 vsnprintf(buf, 256, fmt, ap);
174 va_end(ap);
Tao Bao337db142015-08-20 14:52:57 -0700175
Tao Bao736d59c2017-01-03 10:15:33 -0800176 fputs(buf, stdout);
Tao Bao337db142015-08-20 14:52:57 -0700177
Tao Bao736d59c2017-01-03 10:15:33 -0800178 // This can get called before ui_init(), so be careful.
179 pthread_mutex_lock(&updateMutex);
180 if (text_rows_ > 0 && text_cols_ > 0) {
181 char* ptr;
182 for (ptr = buf; *ptr != '\0'; ++ptr) {
183 if (*ptr == '\n' || text_col_ >= text_cols_) {
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700184 text_[text_row_][text_col_] = '\0';
Tao Bao736d59c2017-01-03 10:15:33 -0800185 text_col_ = 0;
186 text_row_ = (text_row_ + 1) % text_rows_;
187 if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_;
188 }
189 if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr;
Tao Bao337db142015-08-20 14:52:57 -0700190 }
Tao Bao736d59c2017-01-03 10:15:33 -0800191 text_[text_row_][text_col_] = '\0';
192 update_screen_locked();
193 }
194 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700195}
196
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700197void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items,
Damien Bargiacchi5e7cfb92016-08-24 18:28:43 -0700198 int initial_selection) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700199 pthread_mutex_lock(&updateMutex);
200 if (text_rows_ > 0 && text_cols_ > 0) {
201 menu_headers_ = headers;
Tao Baoe15d7a52017-09-07 13:38:51 -0700202 menu_.clear();
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700203 // "i < text_rows_" is removed from the loop termination condition,
204 // which is different from the one in ScreenRecoveryUI::StartMenu().
205 // Because WearRecoveryUI supports scrollable menu, it's fine to have
206 // more entries than text_rows_. The menu may be truncated otherwise.
207 // Bug: 23752519
Tao Baoe15d7a52017-09-07 13:38:51 -0700208 for (size_t i = 0; items[i] != nullptr; i++) {
209 menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1)));
Tao Bao337db142015-08-20 14:52:57 -0700210 }
Tao Baoe15d7a52017-09-07 13:38:51 -0700211 menu_items = static_cast<int>(menu_.size());
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700212 show_menu = true;
213 menu_sel = initial_selection;
214 menu_start = 0;
Tao Bao1e27d142017-08-26 08:32:29 -0700215 menu_end = text_rows_ - 1 - kMenuUnusableRows;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700216 if (menu_items <= menu_end) menu_end = menu_items;
217 update_screen_locked();
218 }
219 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700220}
221
222int WearRecoveryUI::SelectMenu(int sel) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700223 int old_sel;
224 pthread_mutex_lock(&updateMutex);
225 if (show_menu) {
226 old_sel = menu_sel;
227 menu_sel = sel;
228 if (menu_sel < 0) menu_sel = 0;
229 if (menu_sel >= menu_items) menu_sel = menu_items - 1;
230 if (menu_sel < menu_start) {
231 menu_start--;
232 menu_end--;
233 } else if (menu_sel >= menu_end && menu_sel < menu_items) {
234 menu_end++;
235 menu_start++;
Tao Bao337db142015-08-20 14:52:57 -0700236 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700237 sel = menu_sel;
238 if (menu_sel != old_sel) update_screen_locked();
239 }
240 pthread_mutex_unlock(&updateMutex);
241 return sel;
Tao Bao337db142015-08-20 14:52:57 -0700242}
243
Tao Bao337db142015-08-20 14:52:57 -0700244void WearRecoveryUI::ShowFile(FILE* fp) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700245 std::vector<off_t> offsets;
246 offsets.push_back(ftello(fp));
247 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700248
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700249 struct stat sb;
250 fstat(fileno(fp), &sb);
Tao Bao337db142015-08-20 14:52:57 -0700251
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700252 bool show_prompt = false;
253 while (true) {
254 if (show_prompt) {
255 Print("--(%d%% of %d bytes)--",
256 static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))),
257 static_cast<int>(sb.st_size));
258 Redraw();
259 while (show_prompt) {
260 show_prompt = false;
261 int key = WaitKey();
262 if (key == KEY_POWER || key == KEY_ENTER) {
263 return;
264 } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
265 if (offsets.size() <= 1) {
Tao Bao337db142015-08-20 14:52:57 -0700266 show_prompt = true;
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700267 } else {
268 offsets.pop_back();
269 fseek(fp, offsets.back(), SEEK_SET);
270 }
Tao Bao337db142015-08-20 14:52:57 -0700271 } else {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700272 if (feof(fp)) {
273 return;
274 }
275 offsets.push_back(ftello(fp));
Tao Bao337db142015-08-20 14:52:57 -0700276 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700277 }
278 ClearText();
Tao Bao337db142015-08-20 14:52:57 -0700279 }
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700280
281 int ch = getc(fp);
282 if (ch == EOF) {
283 text_row_ = text_top_ = text_rows_ - 2;
284 show_prompt = true;
285 } else {
286 PutChar(ch);
287 if (text_col_ == 0 && text_row_ >= text_rows_ - 2) {
288 text_top_ = text_row_;
289 show_prompt = true;
290 }
291 }
292 }
Tao Bao337db142015-08-20 14:52:57 -0700293}
294
295void WearRecoveryUI::PutChar(char ch) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700296 pthread_mutex_lock(&updateMutex);
297 if (ch != '\n') text_[text_row_][text_col_++] = ch;
298 if (ch == '\n' || text_col_ >= text_cols_) {
299 text_col_ = 0;
300 ++text_row_;
301 }
302 pthread_mutex_unlock(&updateMutex);
Tao Bao337db142015-08-20 14:52:57 -0700303}
304
305void WearRecoveryUI::ShowFile(const char* filename) {
Tao Bao5d2e3bd2017-06-23 22:23:50 -0700306 FILE* fp = fopen_path(filename, "re");
307 if (fp == nullptr) {
308 Print(" Unable to open %s: %s\n", filename, strerror(errno));
309 return;
310 }
311 ShowFile(fp);
312 fclose(fp);
Tao Bao337db142015-08-20 14:52:57 -0700313}