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 | f2be3bd | 2017-08-11 13:50:24 -0700 | [diff] [blame] | 54 | : kProgressBarBaseline(RECOVERY_UI_PROGRESS_BAR_BASELINE), |
| 55 | kMenuUnusableRows(RECOVERY_UI_MENU_UNUSABLE_ROWS) { |
| 56 | // TODO: kMenuUnusableRows should be computed based on the lines in draw_screen_locked(). |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 57 | |
| 58 | // TODO: The following three variables are likely not needed. The first two are detected |
| 59 | // automatically in ScreenRecoveryUI::LoadAnimation(), based on the actual files seen on device. |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 60 | intro_frames = 22; |
| 61 | loop_frames = 60; |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 62 | |
| 63 | touch_screen_allowed_ = true; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 64 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 65 | for (size_t i = 0; i < 5; i++) backgroundIcon[i] = NULL; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 66 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 67 | self = this; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 70 | int WearRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 71 | return kProgressBarBaseline; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 74 | // Draw background frame on the screen. Does not flip pages. |
| 75 | // Should only be called with updateMutex locked. |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 76 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 77 | void WearRecoveryUI::draw_background_locked() { |
| 78 | pagesIdentical = false; |
| 79 | gr_color(0, 0, 0, 255); |
| 80 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 81 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 82 | if (currentIcon != NONE) { |
| 83 | GRSurface* surface; |
| 84 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
| 85 | if (!intro_done) { |
| 86 | surface = introFrames[current_frame]; |
| 87 | } else { |
| 88 | surface = loopFrames[current_frame]; |
| 89 | } |
| 90 | } else { |
| 91 | surface = backgroundIcon[currentIcon]; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 92 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 93 | |
| 94 | int width = gr_get_width(surface); |
| 95 | int height = gr_get_height(surface); |
| 96 | |
| 97 | int x = (gr_fb_width() - width) / 2; |
| 98 | int y = (gr_fb_height() - height) / 2; |
| 99 | |
| 100 | gr_blit(surface, 0, 0, width, height, x, y); |
| 101 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 104 | static const char* SWIPE_HELP[] = { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 105 | "Swipe up/down to move.", |
| 106 | "Swipe left/right to select.", |
| 107 | "", |
| 108 | NULL |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 111 | // TODO merge drawing routines with screen_ui |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 112 | void WearRecoveryUI::draw_screen_locked() { |
| 113 | char cur_selection_str[50]; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 114 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 115 | draw_background_locked(); |
| 116 | if (!show_text) { |
| 117 | draw_foreground_locked(); |
| 118 | } else { |
| 119 | SetColor(TEXT_FILL); |
| 120 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 121 | |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 122 | int y = kMarginHeight; |
| 123 | int x = kMarginWidth; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 124 | if (show_menu) { |
| 125 | std::string recovery_fingerprint = |
| 126 | android::base::GetProperty("ro.bootimage.build.fingerprint", ""); |
| 127 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 128 | y += DrawTextLine(x + 4, y, "Android Recovery", true); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 129 | for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 130 | y += DrawTextLine(x + 4, y, chunk.c_str(), false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 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 | // This is actually the help strings. |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 134 | y += DrawTextLines(x + 4, y, SWIPE_HELP); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 135 | SetColor(HEADER); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 136 | y += DrawTextLines(x + 4, y, menu_headers_); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 137 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 138 | // Show the current menu item number in relation to total number if |
| 139 | // items don't fit on the screen. |
| 140 | if (menu_items > menu_end - menu_start) { |
| 141 | sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items); |
| 142 | gr_text(gr_sys_font(), x + 4, y, cur_selection_str, 1); |
| 143 | y += char_height_ + 4; |
| 144 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 145 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 146 | // Menu begins here |
| 147 | SetColor(MENU); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 148 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 149 | for (int i = menu_start; i < menu_end; ++i) { |
| 150 | if (i == menu_sel) { |
| 151 | // draw the highlight bar |
| 152 | SetColor(MENU_SEL_BG); |
| 153 | gr_fill(x, y - 2, gr_fb_width() - x, y + char_height_ + 2); |
| 154 | // white text of selected item |
| 155 | SetColor(MENU_SEL_FG); |
| 156 | if (menu_[i][0]) { |
Tao Bao | 17fa5c7 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 157 | gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 1); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 158 | } |
| 159 | SetColor(MENU); |
| 160 | } else if (menu_[i][0]) { |
Tao Bao | 17fa5c7 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 161 | gr_text(gr_sys_font(), x + 4, y, menu_[i].c_str(), 0); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 162 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 163 | y += char_height_ + 4; |
| 164 | } |
| 165 | SetColor(MENU); |
| 166 | y += 4; |
| 167 | gr_fill(0, y, gr_fb_width(), y + 2); |
| 168 | y += 4; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 169 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 170 | |
| 171 | SetColor(LOG); |
| 172 | |
| 173 | // display from the bottom up, until we hit the top of the |
| 174 | // screen, the bottom of the menu, or we've displayed the |
| 175 | // entire text buffer. |
| 176 | int ty; |
| 177 | int row = (text_top_ + text_rows_ - 1) % text_rows_; |
| 178 | size_t count = 0; |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 179 | 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] | 180 | ty -= char_height_, ++count) { |
| 181 | gr_text(gr_sys_font(), x + 4, ty, text_[row], 0); |
| 182 | --row; |
| 183 | if (row < 0) row = text_rows_ - 1; |
| 184 | } |
| 185 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Damien Bargiacchi | ad8b5a6 | 2016-09-09 08:18:06 -0700 | [diff] [blame] | 188 | // TODO merge drawing routines with screen_ui |
| 189 | void WearRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 190 | draw_screen_locked(); |
| 191 | gr_flip(); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 194 | bool WearRecoveryUI::InitTextParams() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 195 | if (!ScreenRecoveryUI::InitTextParams()) { |
| 196 | return false; |
| 197 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 198 | |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 199 | text_cols_ = (gr_fb_width() - (kMarginWidth * 2)) / char_width_; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 200 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 201 | if (text_rows_ > kMaxRows) text_rows_ = kMaxRows; |
| 202 | if (text_cols_ > kMaxCols) text_cols_ = kMaxCols; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 203 | |
Tao Bao | 016120f | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 204 | visible_text_rows = (gr_fb_height() - (kMarginHeight * 2)) / char_height_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 205 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 206 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 207 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 208 | bool WearRecoveryUI::Init(const std::string& locale) { |
| 209 | if (!ScreenRecoveryUI::Init(locale)) { |
| 210 | return false; |
| 211 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 212 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 213 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
| 214 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
Tim Kryger | 825b6b0 | 2016-11-29 13:33:29 -0800 | [diff] [blame] | 215 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 216 | // This leaves backgroundIcon[INSTALLING_UPDATE] and backgroundIcon[ERASING] |
| 217 | // 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] | 218 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 219 | return true; |
Prashant Malani | f7f9e50 | 2016-03-10 03:40:20 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 222 | void WearRecoveryUI::SetStage(int current, int max) { |
| 223 | } |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 224 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 225 | void WearRecoveryUI::Print(const char* fmt, ...) { |
| 226 | char buf[256]; |
| 227 | va_list ap; |
| 228 | va_start(ap, fmt); |
| 229 | vsnprintf(buf, 256, fmt, ap); |
| 230 | va_end(ap); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 231 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 232 | fputs(buf, stdout); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 233 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 234 | // This can get called before ui_init(), so be careful. |
| 235 | pthread_mutex_lock(&updateMutex); |
| 236 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 237 | char* ptr; |
| 238 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 239 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 240 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 241 | text_col_ = 0; |
| 242 | text_row_ = (text_row_ + 1) % text_rows_; |
| 243 | if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; |
| 244 | } |
| 245 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 246 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 247 | text_[text_row_][text_col_] = '\0'; |
| 248 | update_screen_locked(); |
| 249 | } |
| 250 | pthread_mutex_unlock(&updateMutex); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 253 | void WearRecoveryUI::StartMenu(const char* const* headers, const char* const* items, |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 254 | int initial_selection) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 255 | pthread_mutex_lock(&updateMutex); |
| 256 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 257 | menu_headers_ = headers; |
Tao Bao | 17fa5c7 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 258 | menu_.clear(); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 259 | // "i < text_rows_" is removed from the loop termination condition, |
| 260 | // which is different from the one in ScreenRecoveryUI::StartMenu(). |
| 261 | // Because WearRecoveryUI supports scrollable menu, it's fine to have |
| 262 | // more entries than text_rows_. The menu may be truncated otherwise. |
| 263 | // Bug: 23752519 |
Tao Bao | 17fa5c7 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 264 | for (size_t i = 0; items[i] != nullptr; i++) { |
| 265 | menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); |
Tao Bao | 337db14 | 2015-08-20 14:52:57 -0700 | [diff] [blame] | 266 | } |
Tao Bao | 17fa5c7 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 267 | menu_items = static_cast<int>(menu_.size()); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 268 | show_menu = true; |
| 269 | menu_sel = initial_selection; |
| 270 | menu_start = 0; |
Tao Bao | f2be3bd | 2017-08-11 13:50:24 -0700 | [diff] [blame] | 271 | menu_end = visible_text_rows - 1 - kMenuUnusableRows; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 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 | } |