Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 17 | #include "wear_ui.h" |
| 18 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Tao Bao | cff8269 | 2017-08-26 07:56:44 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 22 | #include <stdarg.h> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 23 | #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 Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 31 | #include <string> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 34 | #include <android-base/properties.h> |
| 35 | #include <android-base/strings.h> |
| 36 | #include <android-base/stringprintf.h> |
| 37 | #include <minui/minui.h> |
| 38 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 39 | #include "common.h" |
| 40 | #include "device.h" |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 41 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 42 | // Return the current time as a double (including fractions of a second). |
| 43 | static double now() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 44 | struct timeval tv; |
| 45 | gettimeofday(&tv, NULL); |
| 46 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 49 | WearRecoveryUI::WearRecoveryUI() |
Tao Bao | eea3af3 | 2017-08-11 13:50:24 -0700 | [diff] [blame] | 50 | : 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 Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 53 | |
| 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 56 | intro_frames = 22; |
| 57 | loop_frames = 60; |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 58 | |
| 59 | touch_screen_allowed_ = true; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 60 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 61 | for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 64 | int WearRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 65 | return kProgressBarBaseline; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 68 | // Draw background frame on the screen. Does not flip pages. |
| 69 | // Should only be called with updateMutex locked. |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 70 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 71 | void 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 75 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 76 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 86 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 87 | |
| 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 98 | static const char* SWIPE_HELP[] = { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 99 | "Swipe up/down to move.", |
| 100 | "Swipe left/right to select.", |
| 101 | "", |
| 102 | NULL |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 105 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 106 | void WearRecoveryUI::draw_screen_locked() { |
| 107 | char cur_selection_str[50]; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 108 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 109 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 115 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 116 | int y = kMarginHeight; |
| 117 | int x = kMarginWidth; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 118 | if (show_menu) { |
| 119 | std::string recovery_fingerprint = |
| 120 | android::base::GetProperty("ro.bootimage.build.fingerprint", ""); |
| 121 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 122 | y += DrawTextLine(x + 4, y, "Android Recovery", true); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 123 | for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 124 | y += DrawTextLine(x + 4, y, chunk.c_str(), false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 125 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 126 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 127 | // This is actually the help strings. |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 128 | y += DrawTextLines(x + 4, y, SWIPE_HELP); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 129 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 130 | y += DrawTextLines(x + 4, y, menu_headers_); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 131 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 132 | // 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 139 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 140 | // Menu begins here |
| 141 | SetColor(MENU); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 142 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 143 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 156 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 157 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 163 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 164 | |
| 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 Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 173 | for (int ty = gr_fb_height() - char_height_ - kMarginHeight; ty > y + 2 && count < text_rows_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 174 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Damien Bargiacchi | ad8b5a6 | 2016-09-09 08:18:06 -0700 | [diff] [blame] | 182 | // TODO merge drawing routines with screen_ui |
| 183 | void WearRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 184 | draw_screen_locked(); |
| 185 | gr_flip(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 188 | bool WearRecoveryUI::InitTextParams() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 189 | if (!ScreenRecoveryUI::InitTextParams()) { |
| 190 | return false; |
| 191 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 192 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 193 | text_cols_ = (gr_fb_width() - (kMarginWidth * 2)) / char_width_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 194 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 195 | if (text_rows_ > kMaxRows) text_rows_ = kMaxRows; |
| 196 | if (text_cols_ > kMaxCols) text_cols_ = kMaxCols; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 197 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 198 | visible_text_rows = (gr_fb_height() - (kMarginHeight * 2)) / char_height_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 199 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 200 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 201 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 202 | bool WearRecoveryUI::Init(const std::string& locale) { |
| 203 | if (!ScreenRecoveryUI::Init(locale)) { |
| 204 | return false; |
| 205 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 206 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 207 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
| 208 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
Tim Kryger | 825b6b0 | 2016-11-29 13:33:29 -0800 | [diff] [blame] | 209 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 210 | // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING] |
| 211 | // as NULL which is fine since draw_background_locked() doesn't use them. |
Tim Kryger | 825b6b0 | 2016-11-29 13:33:29 -0800 | [diff] [blame] | 212 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 213 | return true; |
Prashant Malani | f7f9e50 | 2016-03-10 03:40:20 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 216 | void WearRecoveryUI::SetStage(int current, int max) { |
| 217 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 218 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 219 | void WearRecoveryUI::Print(const char* fmt, ...) { |
| 220 | char buf[256]; |
| 221 | va_list ap; |
| 222 | va_start(ap, fmt); |
| 223 | vsnprintf(buf, 256, fmt, ap); |
| 224 | va_end(ap); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 225 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 226 | fputs(buf, stdout); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 227 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 228 | // This can get called before ui_init(), so be careful. |
| 229 | pthread_mutex_lock(&updateMutex); |
| 230 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 231 | char* ptr; |
| 232 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 233 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 234 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 235 | text_col_ = 0; |
| 236 | text_row_ = (text_row_ + 1) % text_rows_; |
| 237 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
| 238 | } |
| 239 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 240 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 241 | text_[text_row_][text_col_] = '\0'; |
| 242 | update_screen_locked(); |
| 243 | } |
| 244 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 247 | void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items, |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 248 | int initial_selection) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 249 | pthread_mutex_lock(&updateMutex); |
| 250 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 251 | menu_headers_ = headers; |
| 252 | size_t i = 0; |
| 253 | // "i < text_rows_" is removed from the loop termination condition, |
| 254 | // which is different from the one in ScreenRecoveryUI::StartMenu(). |
| 255 | // Because WearRecoveryUI supports scrollable menu, it's fine to have |
| 256 | // more entries than text_rows_. The menu may be truncated otherwise. |
| 257 | // Bug: 23752519 |
| 258 | for (; items[i] != nullptr; i++) { |
| 259 | strncpy(menu_[i], items[i], text_cols_ - 1); |
| 260 | menu_[i][text_cols_ - 1] = '\0'; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 261 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 262 | menu_items = i; |
| 263 | show_menu = true; |
| 264 | menu_sel = initial_selection; |
| 265 | menu_start = 0; |
Tao Bao | eea3af3 | 2017-08-11 13:50:24 -0700 | [diff] [blame] | 266 | menu_end = visible_text_rows - 1 - kMenuUnusableRows; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 267 | if (menu_items <= menu_end) menu_end = menu_items; |
| 268 | update_screen_locked(); |
| 269 | } |
| 270 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int WearRecoveryUI::SelectMenu(int sel) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 274 | int old_sel; |
| 275 | pthread_mutex_lock(&updateMutex); |
| 276 | if (show_menu) { |
| 277 | old_sel = menu_sel; |
| 278 | menu_sel = sel; |
| 279 | if (menu_sel < 0) menu_sel = 0; |
| 280 | if (menu_sel >= menu_items) menu_sel = menu_items - 1; |
| 281 | if (menu_sel < menu_start) { |
| 282 | menu_start--; |
| 283 | menu_end--; |
| 284 | } else if (menu_sel >= menu_end && menu_sel < menu_items) { |
| 285 | menu_end++; |
| 286 | menu_start++; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 287 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 288 | sel = menu_sel; |
| 289 | if (menu_sel != old_sel) update_screen_locked(); |
| 290 | } |
| 291 | pthread_mutex_unlock(&updateMutex); |
| 292 | return sel; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 295 | void WearRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 296 | std::vector<off_t> offsets; |
| 297 | offsets.push_back(ftello(fp)); |
| 298 | ClearText(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 299 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 300 | struct stat sb; |
| 301 | fstat(fileno(fp), &sb); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 302 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 303 | bool show_prompt = false; |
| 304 | while (true) { |
| 305 | if (show_prompt) { |
| 306 | Print("--(%d%% of %d bytes)--", |
| 307 | static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))), |
| 308 | static_cast<int>(sb.st_size)); |
| 309 | Redraw(); |
| 310 | while (show_prompt) { |
| 311 | show_prompt = false; |
| 312 | int key = WaitKey(); |
| 313 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 314 | return; |
| 315 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 316 | if (offsets.size() <= 1) { |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 317 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 318 | } else { |
| 319 | offsets.pop_back(); |
| 320 | fseek(fp, offsets.back(), SEEK_SET); |
| 321 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 322 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 323 | if (feof(fp)) { |
| 324 | return; |
| 325 | } |
| 326 | offsets.push_back(ftello(fp)); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 327 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 328 | } |
| 329 | ClearText(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 330 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 331 | |
| 332 | int ch = getc(fp); |
| 333 | if (ch == EOF) { |
| 334 | text_row_ = text_top_ = text_rows_ - 2; |
| 335 | show_prompt = true; |
| 336 | } else { |
| 337 | PutChar(ch); |
| 338 | if (text_col_ == 0 && text_row_ >= text_rows_ - 2) { |
| 339 | text_top_ = text_row_; |
| 340 | show_prompt = true; |
| 341 | } |
| 342 | } |
| 343 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void WearRecoveryUI::PutChar(char ch) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 347 | pthread_mutex_lock(&updateMutex); |
| 348 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 349 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 350 | text_col_ = 0; |
| 351 | ++text_row_; |
| 352 | } |
| 353 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void WearRecoveryUI::ShowFile(const char* filename) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 357 | FILE* fp = fopen_path(filename, "re"); |
| 358 | if (fp == nullptr) { |
| 359 | Print(" Unable to open %s: %s\n", filename, strerror(errno)); |
| 360 | return; |
| 361 | } |
| 362 | ShowFile(fp); |
| 363 | fclose(fp); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 366 | void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 367 | va_list ap; |
| 368 | va_start(ap, fmt); |
| 369 | PrintV(fmt, false, ap); |
| 370 | va_end(ap); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void WearRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 374 | std::string str; |
| 375 | android::base::StringAppendV(&str, fmt, ap); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 376 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 377 | if (copy_to_stdout) { |
| 378 | fputs(str.c_str(), stdout); |
| 379 | } |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 380 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 381 | pthread_mutex_lock(&updateMutex); |
| 382 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 383 | for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { |
| 384 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 385 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 386 | text_col_ = 0; |
| 387 | text_row_ = (text_row_ + 1) % text_rows_; |
| 388 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
| 389 | } |
| 390 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 391 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 392 | text_[text_row_][text_col_] = '\0'; |
| 393 | update_screen_locked(); |
| 394 | } |
| 395 | pthread_mutex_unlock(&updateMutex); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 396 | } |