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 | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 21 | #include <stdarg.h> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 22 | #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 Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 30 | #include <string> |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 33 | #include <android-base/properties.h> |
| 34 | #include <android-base/strings.h> |
| 35 | #include <android-base/stringprintf.h> |
| 36 | #include <minui/minui.h> |
| 37 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 38 | #include "common.h" |
| 39 | #include "device.h" |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 40 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 41 | // 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. |
| 44 | static WearRecoveryUI* self = NULL; |
| 45 | |
| 46 | // Return the current time as a double (including fractions of a second). |
| 47 | static double now() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 48 | struct timeval tv; |
| 49 | gettimeofday(&tv, NULL); |
| 50 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 53 | WearRecoveryUI::WearRecoveryUI() |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 54 | : 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 59 | intro_frames = 22; |
| 60 | loop_frames = 60; |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 61 | |
| 62 | touch_screen_allowed_ = true; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 63 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 64 | for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 65 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 66 | self = this; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 69 | int WearRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 70 | return kProgressBarBaseline; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 73 | // Draw background frame on the screen. Does not flip pages. |
| 74 | // Should only be called with updateMutex locked. |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 75 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 76 | void 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 80 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 81 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 91 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 92 | |
| 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 103 | static const char* SWIPE_HELP[] = { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 104 | "Swipe up/down to move.", |
| 105 | "Swipe left/right to select.", |
| 106 | "", |
| 107 | NULL |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 110 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 111 | void WearRecoveryUI::draw_screen_locked() { |
| 112 | char cur_selection_str[50]; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 113 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 114 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 120 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 121 | int y = kMarginHeight; |
| 122 | int x = kMarginWidth; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 123 | if (show_menu) { |
| 124 | std::string recovery_fingerprint = |
| 125 | android::base::GetProperty("ro.bootimage.build.fingerprint", ""); |
| 126 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 127 | y += DrawTextLine(x + 4, y, "Android Recovery", true); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 128 | for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 129 | y += DrawTextLine(x + 4, y, chunk.c_str(), false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 130 | } |
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 | // This is actually the help strings. |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 133 | y += DrawTextLines(x + 4, y, SWIPE_HELP); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 134 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 135 | y += DrawTextLines(x + 4, y, menu_headers_); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 136 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 137 | // 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 144 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 145 | // Menu begins here |
| 146 | SetColor(MENU); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 147 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 148 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 161 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 162 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 168 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 169 | |
| 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 Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 178 | 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] | 179 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Damien Bargiacchi | ad8b5a6 | 2016-09-09 08:18:06 -0700 | [diff] [blame] | 187 | // TODO merge drawing routines with screen_ui |
| 188 | void WearRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 189 | draw_screen_locked(); |
| 190 | gr_flip(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 193 | bool WearRecoveryUI::InitTextParams() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 194 | if (!ScreenRecoveryUI::InitTextParams()) { |
| 195 | return false; |
| 196 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 197 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 198 | text_cols_ = (gr_fb_width() - (kMarginWidth * 2)) / char_width_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 199 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 200 | if (text_rows_ > kMaxRows) text_rows_ = kMaxRows; |
| 201 | if (text_cols_ > kMaxCols) text_cols_ = kMaxCols; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 202 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame^] | 203 | visible_text_rows = (gr_fb_height() - (kMarginHeight * 2)) / char_height_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 204 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 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 | bool WearRecoveryUI::Init(const std::string& locale) { |
| 208 | if (!ScreenRecoveryUI::Init(locale)) { |
| 209 | return false; |
| 210 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 211 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 212 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
| 213 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
Tim Kryger | 825b6b0 | 2016-11-29 13:33:29 -0800 | [diff] [blame] | 214 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 215 | // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING] |
| 216 | // 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] | 217 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 218 | return true; |
Prashant Malani | f7f9e50 | 2016-03-10 03:40:20 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 221 | void WearRecoveryUI::SetStage(int current, int max) { |
| 222 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 223 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 224 | void 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 230 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 231 | fputs(buf, stdout); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 232 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 233 | // 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 Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 239 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 240 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 245 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 246 | text_[text_row_][text_col_] = '\0'; |
| 247 | update_screen_locked(); |
| 248 | } |
| 249 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 252 | void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items, |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 253 | int initial_selection) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 254 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 266 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 267 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | int WearRecoveryUI::SelectMenu(int sel) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 279 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 292 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 293 | sel = menu_sel; |
| 294 | if (menu_sel != old_sel) update_screen_locked(); |
| 295 | } |
| 296 | pthread_mutex_unlock(&updateMutex); |
| 297 | return sel; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 300 | void WearRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 301 | std::vector<off_t> offsets; |
| 302 | offsets.push_back(ftello(fp)); |
| 303 | ClearText(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 304 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 305 | struct stat sb; |
| 306 | fstat(fileno(fp), &sb); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 307 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 308 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 322 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 323 | } else { |
| 324 | offsets.pop_back(); |
| 325 | fseek(fp, offsets.back(), SEEK_SET); |
| 326 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 327 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 328 | if (feof(fp)) { |
| 329 | return; |
| 330 | } |
| 331 | offsets.push_back(ftello(fp)); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 332 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 333 | } |
| 334 | ClearText(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 335 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 336 | |
| 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void WearRecoveryUI::PutChar(char ch) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 352 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void WearRecoveryUI::ShowFile(const char* filename) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 362 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 371 | void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 372 | va_list ap; |
| 373 | va_start(ap, fmt); |
| 374 | PrintV(fmt, false, ap); |
| 375 | va_end(ap); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | 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] | 379 | std::string str; |
| 380 | android::base::StringAppendV(&str, fmt, ap); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 381 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 382 | if (copy_to_stdout) { |
| 383 | fputs(str.c_str(), stdout); |
| 384 | } |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 385 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 386 | 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 Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 390 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 391 | 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 Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 396 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 397 | text_[text_row_][text_col_] = '\0'; |
| 398 | update_screen_locked(); |
| 399 | } |
| 400 | pthread_mutex_unlock(&updateMutex); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 401 | } |