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 | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 42 | WearRecoveryUI::WearRecoveryUI() |
Tao Bao | eea3af3 | 2017-08-11 13:50:24 -0700 | [diff] [blame] | 43 | : 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 Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 46 | |
| 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 49 | intro_frames = 22; |
| 50 | loop_frames = 60; |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 51 | |
| 52 | touch_screen_allowed_ = true; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 53 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 54 | for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 57 | int WearRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 58 | return kProgressBarBaseline; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 61 | // Draw background frame on the screen. Does not flip pages. |
| 62 | // Should only be called with updateMutex locked. |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 63 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 64 | void 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 68 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 69 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 79 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 80 | |
| 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 91 | static const char* SWIPE_HELP[] = { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 92 | "Swipe up/down to move.", |
| 93 | "Swipe left/right to select.", |
| 94 | "", |
| 95 | NULL |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 98 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 99 | void WearRecoveryUI::draw_screen_locked() { |
| 100 | char cur_selection_str[50]; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 101 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 102 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 108 | |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 109 | int y = kMarginHeight; |
| 110 | int x = kMarginWidth; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 111 | if (show_menu) { |
| 112 | std::string recovery_fingerprint = |
| 113 | android::base::GetProperty("ro.bootimage.build.fingerprint", ""); |
| 114 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 115 | y += DrawTextLine(x + 4, y, "Android Recovery", true); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 116 | for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 117 | y += DrawTextLine(x + 4, y, chunk.c_str(), false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 118 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 119 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 120 | // This is actually the help strings. |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 121 | y += DrawTextLines(x + 4, y, SWIPE_HELP); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 122 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 123 | y += DrawTextLines(x + 4, y, menu_headers_); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 124 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 125 | // 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 132 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 133 | // Menu begins here |
| 134 | SetColor(MENU); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 135 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 136 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 149 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 150 | 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 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 | |
| 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 Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 163 | int row = (text_top_ + text_rows_ - 1) % text_rows_; |
| 164 | size_t count = 0; |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 165 | 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] | 166 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Damien Bargiacchi | ad8b5a6 | 2016-09-09 08:18:06 -0700 | [diff] [blame] | 174 | // TODO merge drawing routines with screen_ui |
| 175 | void WearRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 176 | draw_screen_locked(); |
| 177 | gr_flip(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 180 | bool WearRecoveryUI::Init(const std::string& locale) { |
| 181 | if (!ScreenRecoveryUI::Init(locale)) { |
| 182 | return false; |
| 183 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 184 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 185 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
| 186 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
Tim Kryger | 825b6b0 | 2016-11-29 13:33:29 -0800 | [diff] [blame] | 187 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 188 | // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING] |
| 189 | // 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] | 190 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 191 | return true; |
Prashant Malani | f7f9e50 | 2016-03-10 03:40:20 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 194 | void WearRecoveryUI::SetStage(int current, int max) { |
| 195 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 196 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 197 | void 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 203 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 204 | fputs(buf, stdout); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 205 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 206 | // 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 Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 212 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 213 | 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 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 | text_[text_row_][text_col_] = '\0'; |
| 220 | update_screen_locked(); |
| 221 | } |
| 222 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 225 | void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items, |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 226 | int initial_selection) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 227 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 239 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 240 | menu_items = i; |
| 241 | show_menu = true; |
| 242 | menu_sel = initial_selection; |
| 243 | menu_start = 0; |
Tao Bao | 1e27d14 | 2017-08-26 08:32:29 -0700 | [diff] [blame] | 244 | menu_end = text_rows_ - 1 - kMenuUnusableRows; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 245 | if (menu_items <= menu_end) menu_end = menu_items; |
| 246 | update_screen_locked(); |
| 247 | } |
| 248 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | int WearRecoveryUI::SelectMenu(int sel) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 252 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 265 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 266 | sel = menu_sel; |
| 267 | if (menu_sel != old_sel) update_screen_locked(); |
| 268 | } |
| 269 | pthread_mutex_unlock(&updateMutex); |
| 270 | return sel; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 273 | void WearRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 274 | std::vector<off_t> offsets; |
| 275 | offsets.push_back(ftello(fp)); |
| 276 | ClearText(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 277 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 278 | struct stat sb; |
| 279 | fstat(fileno(fp), &sb); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 280 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 281 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 295 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 296 | } else { |
| 297 | offsets.pop_back(); |
| 298 | fseek(fp, offsets.back(), SEEK_SET); |
| 299 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 300 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 301 | if (feof(fp)) { |
| 302 | return; |
| 303 | } |
| 304 | offsets.push_back(ftello(fp)); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 305 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 306 | } |
| 307 | ClearText(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 308 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 309 | |
| 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void WearRecoveryUI::PutChar(char ch) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 325 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void WearRecoveryUI::ShowFile(const char* filename) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 335 | 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 Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 344 | void WearRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 345 | va_list ap; |
| 346 | va_start(ap, fmt); |
| 347 | PrintV(fmt, false, ap); |
| 348 | va_end(ap); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | 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] | 352 | std::string str; |
| 353 | android::base::StringAppendV(&str, fmt, ap); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 354 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 355 | if (copy_to_stdout) { |
| 356 | fputs(str.c_str(), stdout); |
| 357 | } |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 358 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 359 | 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 Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 363 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 364 | 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 Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 369 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 370 | text_[text_row_][text_col_] = '\0'; |
| 371 | update_screen_locked(); |
| 372 | } |
| 373 | pthread_mutex_unlock(&updateMutex); |
Prashant Malani | 0eb41c3 | 2016-02-25 18:27:03 -0800 | [diff] [blame] | 374 | } |