Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <linux/input.h> |
| 21 | #include <pthread.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 32 | #include <string> |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 39 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 40 | #include "common.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 41 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 42 | #include "minui/minui.h" |
| 43 | #include "screen_ui.h" |
| 44 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 45 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 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, nullptr); |
| 50 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 53 | ScreenRecoveryUI::ScreenRecoveryUI() |
Tao Bao | 4521b70 | 2017-06-20 18:11:21 -0700 | [diff] [blame] | 54 | : kMarginWidth(RECOVERY_UI_MARGIN_WIDTH), |
| 55 | kMarginHeight(RECOVERY_UI_MARGIN_HEIGHT), |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 56 | kAnimationFps(RECOVERY_UI_ANIMATION_FPS), |
Tao Bao | 7577965 | 2017-09-10 11:28:32 -0700 | [diff] [blame] | 57 | kDensity(static_cast<float>(android::base::GetIntProperty("ro.sf.lcd_density", 160)) / 160.f), |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 58 | currentIcon(NONE), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 59 | progressBarType(EMPTY), |
| 60 | progressScopeStart(0), |
| 61 | progressScopeSize(0), |
| 62 | progress(0), |
| 63 | pagesIdentical(false), |
| 64 | text_cols_(0), |
| 65 | text_rows_(0), |
| 66 | text_(nullptr), |
| 67 | text_col_(0), |
| 68 | text_row_(0), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 69 | show_text(false), |
| 70 | show_text_ever(false), |
Tao Bao | e15d7a5 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 71 | menu_headers_(nullptr), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 72 | show_menu(false), |
| 73 | menu_items(0), |
| 74 | menu_sel(0), |
| 75 | file_viewer_text_(nullptr), |
| 76 | intro_frames(0), |
| 77 | loop_frames(0), |
| 78 | current_frame(0), |
| 79 | intro_done(false), |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 80 | stage(-1), |
| 81 | max_stage(-1), |
| 82 | updateMutex(PTHREAD_MUTEX_INITIALIZER) {} |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 83 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 84 | GRSurface* ScreenRecoveryUI::GetCurrentFrame() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 85 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
| 86 | return intro_done ? loopFrames[current_frame] : introFrames[current_frame]; |
| 87 | } |
| 88 | return error_icon; |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 91 | GRSurface* ScreenRecoveryUI::GetCurrentText() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 92 | switch (currentIcon) { |
| 93 | case ERASING: |
| 94 | return erasing_text; |
| 95 | case ERROR: |
| 96 | return error_text; |
| 97 | case INSTALLING_UPDATE: |
| 98 | return installing_text; |
| 99 | case NO_COMMAND: |
| 100 | return no_command_text; |
| 101 | case NONE: |
| 102 | abort(); |
| 103 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 106 | int ScreenRecoveryUI::PixelsFromDp(int dp) const { |
Tao Bao | 7577965 | 2017-09-10 11:28:32 -0700 | [diff] [blame] | 107 | return dp * kDensity; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Here's the intended layout: |
| 111 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 112 | // | portrait large landscape large |
| 113 | // ---------+------------------------------------------------- |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 114 | // gap | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 115 | // icon | (200dp) |
| 116 | // gap | 68dp 68dp 56dp 112dp |
| 117 | // text | (14sp) |
| 118 | // gap | 32dp 32dp 26dp 52dp |
| 119 | // progress | (2dp) |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 120 | // gap | |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 121 | |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 122 | // Note that "baseline" is actually the *top* of each icon (because that's how our drawing routines |
| 123 | // work), so that's the more useful measurement for calling code. We use even top and bottom gaps. |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 124 | |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 125 | enum Layout { PORTRAIT = 0, PORTRAIT_LARGE = 1, LANDSCAPE = 2, LANDSCAPE_LARGE = 3, LAYOUT_MAX }; |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 126 | enum Dimension { TEXT = 0, ICON = 1, DIMENSION_MAX }; |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 127 | static constexpr int kLayouts[LAYOUT_MAX][DIMENSION_MAX] = { |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 128 | { 32, 68, }, // PORTRAIT |
| 129 | { 32, 68, }, // PORTRAIT_LARGE |
| 130 | { 26, 56, }, // LANDSCAPE |
| 131 | { 52, 112, }, // LANDSCAPE_LARGE |
Elliott Hughes | 6d089a9 | 2016-07-08 17:23:41 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 134 | int ScreenRecoveryUI::GetAnimationBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 135 | return GetTextBaseline() - PixelsFromDp(kLayouts[layout_][ICON]) - gr_get_height(loopFrames[0]); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 138 | int ScreenRecoveryUI::GetTextBaseline() const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 139 | return GetProgressBaseline() - PixelsFromDp(kLayouts[layout_][TEXT]) - |
| 140 | gr_get_height(installing_text); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 143 | int ScreenRecoveryUI::GetProgressBaseline() const { |
Tao Bao | 3250f72 | 2017-06-29 14:32:05 -0700 | [diff] [blame] | 144 | int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) + |
| 145 | gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) + |
| 146 | gr_get_height(progressBarFill); |
| 147 | int bottom_gap = (gr_fb_height() - elements_sum) / 2; |
| 148 | return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill); |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 151 | // Clear the screen and draw the currently selected background icon (if any). |
| 152 | // Should only be called with updateMutex locked. |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 153 | void ScreenRecoveryUI::draw_background_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 154 | pagesIdentical = false; |
| 155 | gr_color(0, 0, 0, 255); |
| 156 | gr_clear(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 157 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 158 | if (currentIcon != NONE) { |
| 159 | if (max_stage != -1) { |
| 160 | int stage_height = gr_get_height(stageMarkerEmpty); |
| 161 | int stage_width = gr_get_width(stageMarkerEmpty); |
| 162 | int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2; |
| 163 | int y = gr_fb_height() - stage_height; |
| 164 | for (int i = 0; i < max_stage; ++i) { |
| 165 | GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty; |
| 166 | gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y); |
| 167 | x += stage_width; |
| 168 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 169 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 170 | |
| 171 | GRSurface* text_surface = GetCurrentText(); |
| 172 | int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2; |
| 173 | int text_y = GetTextBaseline(); |
| 174 | gr_color(255, 255, 255, 255); |
| 175 | gr_texticon(text_x, text_y, text_surface); |
| 176 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 179 | // Draws the animation and progress bar (if any) on the screen. Does not flip pages. Should only be |
| 180 | // called with updateMutex locked. |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 181 | void ScreenRecoveryUI::draw_foreground_locked() { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 182 | if (currentIcon != NONE) { |
| 183 | GRSurface* frame = GetCurrentFrame(); |
| 184 | int frame_width = gr_get_width(frame); |
| 185 | int frame_height = gr_get_height(frame); |
| 186 | int frame_x = (gr_fb_width() - frame_width) / 2; |
| 187 | int frame_y = GetAnimationBaseline(); |
| 188 | gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y); |
| 189 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 190 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 191 | if (progressBarType != EMPTY) { |
| 192 | int width = gr_get_width(progressBarEmpty); |
| 193 | int height = gr_get_height(progressBarEmpty); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 194 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 195 | int progress_x = (gr_fb_width() - width) / 2; |
| 196 | int progress_y = GetProgressBaseline(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 197 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 198 | // Erase behind the progress bar (in case this was a progress-only update) |
| 199 | gr_color(0, 0, 0, 255); |
| 200 | gr_fill(progress_x, progress_y, width, height); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 201 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 202 | if (progressBarType == DETERMINATE) { |
| 203 | float p = progressScopeStart + progress * progressScopeSize; |
| 204 | int pos = static_cast<int>(p * width); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 205 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 206 | if (rtl_locale_) { |
| 207 | // Fill the progress bar from right to left. |
| 208 | if (pos > 0) { |
| 209 | gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos, |
| 210 | progress_y); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 211 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 212 | if (pos < width - 1) { |
| 213 | gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y); |
| 214 | } |
| 215 | } else { |
| 216 | // Fill the progress bar from left to right. |
| 217 | if (pos > 0) { |
| 218 | gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y); |
| 219 | } |
| 220 | if (pos < width - 1) { |
| 221 | gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y); |
| 222 | } |
| 223 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 224 | } |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 225 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Tao Bao | 99b2d77 | 2017-06-23 22:47:03 -0700 | [diff] [blame] | 228 | void ScreenRecoveryUI::SetColor(UIElement e) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 229 | switch (e) { |
| 230 | case INFO: |
| 231 | gr_color(249, 194, 0, 255); |
| 232 | break; |
| 233 | case HEADER: |
| 234 | gr_color(247, 0, 6, 255); |
| 235 | break; |
| 236 | case MENU: |
| 237 | case MENU_SEL_BG: |
| 238 | gr_color(0, 106, 157, 255); |
| 239 | break; |
| 240 | case MENU_SEL_BG_ACTIVE: |
| 241 | gr_color(0, 156, 100, 255); |
| 242 | break; |
| 243 | case MENU_SEL_FG: |
| 244 | gr_color(255, 255, 255, 255); |
| 245 | break; |
| 246 | case LOG: |
| 247 | gr_color(196, 196, 196, 255); |
| 248 | break; |
| 249 | case TEXT_FILL: |
| 250 | gr_color(0, 0, 0, 160); |
| 251 | break; |
| 252 | default: |
| 253 | gr_color(255, 255, 255, 255); |
| 254 | break; |
| 255 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 256 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 257 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 258 | int ScreenRecoveryUI::DrawHorizontalRule(int y) const { |
| 259 | gr_fill(0, y + 4, gr_fb_width(), y + 6); |
| 260 | return 8; |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 263 | void ScreenRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 264 | gr_fill(x, y, x + width, y + height); |
Luke Song | e2bd876 | 2017-06-12 16:08:33 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 267 | int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const { |
| 268 | gr_text(gr_sys_font(), x, y, line, bold); |
| 269 | return char_height_ + 4; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 272 | int ScreenRecoveryUI::DrawTextLines(int x, int y, const char* const* lines) const { |
| 273 | int offset = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 274 | for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 275 | offset += DrawTextLine(x, y + offset, lines[i], false); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 276 | } |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 277 | return offset; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 280 | int ScreenRecoveryUI::DrawWrappedTextLines(int x, int y, const char* const* lines) const { |
| 281 | int offset = 0; |
| 282 | for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { |
| 283 | // The line will be wrapped if it exceeds text_cols_. |
| 284 | std::string line(lines[i]); |
| 285 | size_t next_start = 0; |
| 286 | while (next_start < line.size()) { |
| 287 | std::string sub = line.substr(next_start, text_cols_ + 1); |
| 288 | if (sub.size() <= text_cols_) { |
| 289 | next_start += sub.size(); |
| 290 | } else { |
| 291 | // Line too long and must be wrapped to text_cols_ columns. |
| 292 | size_t last_space = sub.find_last_of(" \t\n"); |
| 293 | if (last_space == std::string::npos) { |
| 294 | // No space found, just draw as much as we can |
| 295 | sub.resize(text_cols_); |
| 296 | next_start += text_cols_; |
| 297 | } else { |
| 298 | sub.resize(last_space); |
| 299 | next_start += last_space + 1; |
| 300 | } |
| 301 | } |
| 302 | offset += DrawTextLine(x, y + offset, sub.c_str(), false); |
| 303 | } |
| 304 | } |
| 305 | return offset; |
| 306 | } |
| 307 | |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 308 | static const char* REGULAR_HELP[] = { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 309 | "Use volume up/down and power.", |
| 310 | NULL |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | static const char* LONG_PRESS_HELP[] = { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 314 | "Any button cycles highlight.", |
| 315 | "Long-press activates.", |
| 316 | NULL |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 317 | }; |
| 318 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 319 | // Redraws everything on the screen. Does not flip pages. Should only be called with updateMutex |
| 320 | // locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 321 | void ScreenRecoveryUI::draw_screen_locked() { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 322 | if (!show_text) { |
| 323 | draw_background_locked(); |
| 324 | draw_foreground_locked(); |
| 325 | return; |
| 326 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 327 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 328 | gr_color(0, 0, 0, 255); |
| 329 | gr_clear(); |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 330 | |
Tao Bao | 4521b70 | 2017-06-20 18:11:21 -0700 | [diff] [blame] | 331 | int y = kMarginHeight; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 332 | if (show_menu) { |
Tao Bao | ca6ce2c | 2017-07-13 11:15:27 -0700 | [diff] [blame] | 333 | static constexpr int kMenuIndent = 4; |
| 334 | int x = kMarginWidth + kMenuIndent; |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 335 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 336 | SetColor(INFO); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 337 | y += DrawTextLine(x, y, "Android Recovery", true); |
Tao Bao | ca6ce2c | 2017-07-13 11:15:27 -0700 | [diff] [blame] | 338 | std::string recovery_fingerprint = |
| 339 | android::base::GetProperty("ro.bootimage.build.fingerprint", ""); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 340 | for (const auto& chunk : android::base::Split(recovery_fingerprint, ":")) { |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 341 | y += DrawTextLine(x, y, chunk.c_str(), false); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 342 | } |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 343 | y += DrawTextLines(x, y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 344 | |
| 345 | SetColor(HEADER); |
Tao Bao | 13aa4a9 | 2017-08-16 13:25:55 -0700 | [diff] [blame] | 346 | // Ignore kMenuIndent, which is not taken into account by text_cols_. |
| 347 | y += DrawWrappedTextLines(kMarginWidth, y, menu_headers_); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 348 | |
| 349 | SetColor(MENU); |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 350 | y += DrawHorizontalRule(y) + 4; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 351 | for (int i = 0; i < menu_items; ++i) { |
| 352 | if (i == menu_sel) { |
| 353 | // Draw the highlight bar. |
| 354 | SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); |
| 355 | DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4); |
| 356 | // Bold white text for the selected item. |
| 357 | SetColor(MENU_SEL_FG); |
Tao Bao | e15d7a5 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 358 | y += DrawTextLine(x, y, menu_[i].c_str(), true); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 359 | SetColor(MENU); |
| 360 | } else { |
Tao Bao | e15d7a5 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 361 | y += DrawTextLine(x, y, menu_[i].c_str(), false); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
Tao Bao | ea78d86 | 2017-06-28 14:52:17 -0700 | [diff] [blame] | 364 | y += DrawHorizontalRule(y); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Display from the bottom up, until we hit the top of the screen, the bottom of the menu, or |
| 368 | // we've displayed the entire text buffer. |
| 369 | SetColor(LOG); |
Tao Bao | cb5524c | 2017-09-08 21:25:32 -0700 | [diff] [blame] | 370 | int row = text_row_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 371 | size_t count = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 372 | for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_; |
| 373 | ty -= char_height_, ++count) { |
Tao Bao | ca6ce2c | 2017-07-13 11:15:27 -0700 | [diff] [blame] | 374 | DrawTextLine(kMarginWidth, ty, text_[row], false); |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 375 | --row; |
| 376 | if (row < 0) row = text_rows_ - 1; |
| 377 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Redraw everything on the screen and flip the screen (make it visible). |
| 381 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 382 | void ScreenRecoveryUI::update_screen_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 383 | draw_screen_locked(); |
| 384 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 388 | // Should only be called with updateMutex locked. |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 389 | void ScreenRecoveryUI::update_progress_locked() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 390 | if (show_text || !pagesIdentical) { |
| 391 | draw_screen_locked(); // Must redraw the whole screen |
| 392 | pagesIdentical = true; |
| 393 | } else { |
| 394 | draw_foreground_locked(); // Draw only the progress bar and overlays |
| 395 | } |
| 396 | gr_flip(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 400 | void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 401 | reinterpret_cast<ScreenRecoveryUI*>(data)->ProgressThreadLoop(); |
| 402 | return nullptr; |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Elliott Hughes | 985022a | 2015-04-13 13:04:32 -0700 | [diff] [blame] | 405 | void ScreenRecoveryUI::ProgressThreadLoop() { |
Tao Bao | 0470cee | 2017-08-02 17:11:04 -0700 | [diff] [blame] | 406 | double interval = 1.0 / kAnimationFps; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 407 | while (true) { |
| 408 | double start = now(); |
| 409 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 410 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 411 | bool redraw = false; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 412 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 413 | // update the installation animation, if active |
| 414 | // skip this if we have a text overlay (too expensive to update) |
| 415 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && !show_text) { |
| 416 | if (!intro_done) { |
| 417 | if (current_frame == intro_frames - 1) { |
| 418 | intro_done = true; |
| 419 | current_frame = 0; |
| 420 | } else { |
| 421 | ++current_frame; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 422 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 423 | } else { |
| 424 | current_frame = (current_frame + 1) % loop_frames; |
| 425 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 426 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 427 | redraw = true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 428 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 429 | |
| 430 | // move the progress bar forward on timed intervals, if configured |
| 431 | int duration = progressScopeDuration; |
| 432 | if (progressBarType == DETERMINATE && duration > 0) { |
| 433 | double elapsed = now() - progressScopeTime; |
| 434 | float p = 1.0 * elapsed / duration; |
| 435 | if (p > 1.0) p = 1.0; |
| 436 | if (p > progress) { |
| 437 | progress = p; |
| 438 | redraw = true; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (redraw) update_progress_locked(); |
| 443 | |
| 444 | pthread_mutex_unlock(&updateMutex); |
| 445 | double end = now(); |
| 446 | // minimum of 20ms delay between frames |
| 447 | double delay = interval - (end - start); |
| 448 | if (delay < 0.02) delay = 0.02; |
| 449 | usleep(static_cast<useconds_t>(delay * 1000000)); |
| 450 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 453 | void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 454 | int result = res_create_display_surface(filename, surface); |
| 455 | if (result < 0) { |
| 456 | LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")"; |
| 457 | } |
Doug Zongker | eac881c | 2014-03-07 09:21:25 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 460 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 461 | int result = res_create_localized_alpha_surface(filename, locale_.c_str(), surface); |
| 462 | if (result < 0) { |
| 463 | LOG(ERROR) << "couldn't load bitmap " << filename << " (error " << result << ")"; |
| 464 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 467 | static char** Alloc2d(size_t rows, size_t cols) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 468 | char** result = new char*[rows]; |
| 469 | for (size_t i = 0; i < rows; ++i) { |
| 470 | result[i] = new char[cols]; |
| 471 | memset(result[i], 0, cols); |
| 472 | } |
| 473 | return result; |
Elliott Hughes | aa0d6af | 2015-04-08 12:42:50 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 476 | // Choose the right background string to display during update. |
| 477 | void ScreenRecoveryUI::SetSystemUpdateText(bool security_update) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 478 | if (security_update) { |
| 479 | LoadLocalizedBitmap("installing_security_text", &installing_text); |
| 480 | } else { |
| 481 | LoadLocalizedBitmap("installing_text", &installing_text); |
| 482 | } |
| 483 | Redraw(); |
Tianjie Xu | 35926c4 | 2016-04-28 18:06:26 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 486 | bool ScreenRecoveryUI::InitTextParams() { |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 487 | if (gr_init() < 0) { |
| 488 | return false; |
| 489 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 490 | |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 491 | gr_font_size(gr_sys_font(), &char_width_, &char_height_); |
Tao Bao | 4521b70 | 2017-06-20 18:11:21 -0700 | [diff] [blame] | 492 | text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_; |
| 493 | text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_; |
Tao Bao | 171b4c4 | 2017-06-19 23:10:44 -0700 | [diff] [blame] | 494 | return true; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 497 | bool ScreenRecoveryUI::Init(const std::string& locale) { |
| 498 | RecoveryUI::Init(locale); |
| 499 | if (!InitTextParams()) { |
| 500 | return false; |
| 501 | } |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 502 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 503 | // Are we portrait or landscape? |
| 504 | layout_ = (gr_fb_width() > gr_fb_height()) ? LANDSCAPE : PORTRAIT; |
| 505 | // Are we the large variant of our base layout? |
| 506 | if (gr_fb_height() > PixelsFromDp(800)) ++layout_; |
Elliott Hughes | faf36e0 | 2016-04-20 17:22:16 -0700 | [diff] [blame] | 507 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 508 | text_ = Alloc2d(text_rows_, text_cols_ + 1); |
| 509 | file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 510 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 511 | text_col_ = text_row_ = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 512 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 513 | LoadBitmap("icon_error", &error_icon); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 514 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 515 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 516 | LoadBitmap("progress_fill", &progressBarFill); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 517 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 518 | LoadBitmap("stage_empty", &stageMarkerEmpty); |
| 519 | LoadBitmap("stage_fill", &stageMarkerFill); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 520 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 521 | // Background text for "installing_update" could be "installing update" |
| 522 | // or "installing security update". It will be set after UI init according |
| 523 | // to commands in BCB. |
| 524 | installing_text = nullptr; |
| 525 | LoadLocalizedBitmap("erasing_text", &erasing_text); |
| 526 | LoadLocalizedBitmap("no_command_text", &no_command_text); |
| 527 | LoadLocalizedBitmap("error_text", &error_text); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 528 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 529 | LoadAnimation(); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 530 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 531 | pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this); |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 532 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 533 | return true; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 536 | void ScreenRecoveryUI::LoadAnimation() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 537 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/res/images"), closedir); |
| 538 | dirent* de; |
| 539 | std::vector<std::string> intro_frame_names; |
| 540 | std::vector<std::string> loop_frame_names; |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 541 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 542 | while ((de = readdir(dir.get())) != nullptr) { |
| 543 | int value, num_chars; |
| 544 | if (sscanf(de->d_name, "intro%d%n.png", &value, &num_chars) == 1) { |
| 545 | intro_frame_names.emplace_back(de->d_name, num_chars); |
| 546 | } else if (sscanf(de->d_name, "loop%d%n.png", &value, &num_chars) == 1) { |
| 547 | loop_frame_names.emplace_back(de->d_name, num_chars); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 548 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 549 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 550 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 551 | intro_frames = intro_frame_names.size(); |
| 552 | loop_frames = loop_frame_names.size(); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 553 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 554 | // It's okay to not have an intro. |
| 555 | if (intro_frames == 0) intro_done = true; |
| 556 | // But you must have an animation. |
| 557 | if (loop_frames == 0) abort(); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 558 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 559 | std::sort(intro_frame_names.begin(), intro_frame_names.end()); |
| 560 | std::sort(loop_frame_names.begin(), loop_frame_names.end()); |
Damien Bargiacchi | 5e7cfb9 | 2016-08-24 18:28:43 -0700 | [diff] [blame] | 561 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 562 | introFrames = new GRSurface*[intro_frames]; |
| 563 | for (size_t i = 0; i < intro_frames; i++) { |
| 564 | LoadBitmap(intro_frame_names.at(i).c_str(), &introFrames[i]); |
| 565 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 566 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 567 | loopFrames = new GRSurface*[loop_frames]; |
| 568 | for (size_t i = 0; i < loop_frames; i++) { |
| 569 | LoadBitmap(loop_frame_names.at(i).c_str(), &loopFrames[i]); |
| 570 | } |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 573 | void ScreenRecoveryUI::SetBackground(Icon icon) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 574 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 575 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 576 | currentIcon = icon; |
| 577 | update_screen_locked(); |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 578 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 579 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 582 | void ScreenRecoveryUI::SetProgressType(ProgressType type) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 583 | pthread_mutex_lock(&updateMutex); |
| 584 | if (progressBarType != type) { |
| 585 | progressBarType = type; |
| 586 | } |
| 587 | progressScopeStart = 0; |
| 588 | progressScopeSize = 0; |
| 589 | progress = 0; |
| 590 | update_progress_locked(); |
| 591 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 594 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 595 | pthread_mutex_lock(&updateMutex); |
| 596 | progressBarType = DETERMINATE; |
| 597 | progressScopeStart += progressScopeSize; |
| 598 | progressScopeSize = portion; |
| 599 | progressScopeTime = now(); |
| 600 | progressScopeDuration = seconds; |
| 601 | progress = 0; |
| 602 | update_progress_locked(); |
| 603 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 606 | void ScreenRecoveryUI::SetProgress(float fraction) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 607 | pthread_mutex_lock(&updateMutex); |
| 608 | if (fraction < 0.0) fraction = 0.0; |
| 609 | if (fraction > 1.0) fraction = 1.0; |
| 610 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 611 | // Skip updates that aren't visibly different. |
| 612 | int width = gr_get_width(progressBarEmpty); |
| 613 | float scale = width * progressScopeSize; |
| 614 | if ((int)(progress * scale) != (int)(fraction * scale)) { |
| 615 | progress = fraction; |
| 616 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 617 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 618 | } |
| 619 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 622 | void ScreenRecoveryUI::SetStage(int current, int max) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 623 | pthread_mutex_lock(&updateMutex); |
| 624 | stage = current; |
| 625 | max_stage = max; |
| 626 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 629 | void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 630 | std::string str; |
| 631 | android::base::StringAppendV(&str, fmt, ap); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 632 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 633 | if (copy_to_stdout) { |
| 634 | fputs(str.c_str(), stdout); |
| 635 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 636 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 637 | pthread_mutex_lock(&updateMutex); |
| 638 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 639 | for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { |
| 640 | if (*ptr == '\n' || text_col_ >= text_cols_) { |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 641 | text_[text_row_][text_col_] = '\0'; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 642 | text_col_ = 0; |
| 643 | text_row_ = (text_row_ + 1) % text_rows_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 644 | } |
| 645 | if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 646 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 647 | text_[text_row_][text_col_] = '\0'; |
| 648 | update_screen_locked(); |
| 649 | } |
| 650 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 653 | void ScreenRecoveryUI::Print(const char* fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 654 | va_list ap; |
| 655 | va_start(ap, fmt); |
| 656 | PrintV(fmt, true, ap); |
| 657 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 661 | va_list ap; |
| 662 | va_start(ap, fmt); |
| 663 | PrintV(fmt, false, ap); |
| 664 | va_end(ap); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 667 | void ScreenRecoveryUI::PutChar(char ch) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 668 | pthread_mutex_lock(&updateMutex); |
| 669 | if (ch != '\n') text_[text_row_][text_col_++] = ch; |
| 670 | if (ch == '\n' || text_col_ >= text_cols_) { |
| 671 | text_col_ = 0; |
| 672 | ++text_row_; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 673 | } |
| 674 | pthread_mutex_unlock(&updateMutex); |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 677 | void ScreenRecoveryUI::ClearText() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 678 | pthread_mutex_lock(&updateMutex); |
| 679 | text_col_ = 0; |
| 680 | text_row_ = 0; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 681 | for (size_t i = 0; i < text_rows_; ++i) { |
| 682 | memset(text_[i], 0, text_cols_ + 1); |
| 683 | } |
| 684 | pthread_mutex_unlock(&updateMutex); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void ScreenRecoveryUI::ShowFile(FILE* fp) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 688 | std::vector<off_t> offsets; |
| 689 | offsets.push_back(ftello(fp)); |
| 690 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 691 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 692 | struct stat sb; |
| 693 | fstat(fileno(fp), &sb); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 694 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 695 | bool show_prompt = false; |
| 696 | while (true) { |
| 697 | if (show_prompt) { |
| 698 | PrintOnScreenOnly("--(%d%% of %d bytes)--", |
| 699 | static_cast<int>(100 * (double(ftello(fp)) / double(sb.st_size))), |
| 700 | static_cast<int>(sb.st_size)); |
| 701 | Redraw(); |
| 702 | while (show_prompt) { |
| 703 | show_prompt = false; |
| 704 | int key = WaitKey(); |
| 705 | if (key == KEY_POWER || key == KEY_ENTER) { |
| 706 | return; |
| 707 | } else if (key == KEY_UP || key == KEY_VOLUMEUP) { |
| 708 | if (offsets.size() <= 1) { |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 709 | show_prompt = true; |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 710 | } else { |
| 711 | offsets.pop_back(); |
| 712 | fseek(fp, offsets.back(), SEEK_SET); |
| 713 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 714 | } else { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 715 | if (feof(fp)) { |
| 716 | return; |
| 717 | } |
| 718 | offsets.push_back(ftello(fp)); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 719 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 720 | } |
| 721 | ClearText(); |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 722 | } |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 723 | |
| 724 | int ch = getc(fp); |
| 725 | if (ch == EOF) { |
| 726 | while (text_row_ < text_rows_ - 1) PutChar('\n'); |
| 727 | show_prompt = true; |
| 728 | } else { |
| 729 | PutChar(ch); |
| 730 | if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { |
| 731 | show_prompt = true; |
| 732 | } |
| 733 | } |
| 734 | } |
Elliott Hughes | 95fc63e | 2015-04-10 19:12:01 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 737 | void ScreenRecoveryUI::ShowFile(const char* filename) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 738 | FILE* fp = fopen_path(filename, "re"); |
| 739 | if (fp == nullptr) { |
| 740 | Print(" Unable to open %s: %s\n", filename, strerror(errno)); |
| 741 | return; |
| 742 | } |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 743 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 744 | char** old_text = text_; |
| 745 | size_t old_text_col = text_col_; |
| 746 | size_t old_text_row = text_row_; |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 747 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 748 | // Swap in the alternate screen and clear it. |
| 749 | text_ = file_viewer_text_; |
| 750 | ClearText(); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 751 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 752 | ShowFile(fp); |
| 753 | fclose(fp); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 754 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 755 | text_ = old_text; |
| 756 | text_col_ = old_text_col; |
| 757 | text_row_ = old_text_row; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 760 | void ScreenRecoveryUI::StartMenu(const char* const* headers, const char* const* items, |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 761 | int initial_selection) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 762 | pthread_mutex_lock(&updateMutex); |
| 763 | if (text_rows_ > 0 && text_cols_ > 0) { |
| 764 | menu_headers_ = headers; |
Tao Bao | e15d7a5 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 765 | menu_.clear(); |
| 766 | for (size_t i = 0; i < text_rows_ && items[i] != nullptr; ++i) { |
| 767 | menu_.emplace_back(std::string(items[i], strnlen(items[i], text_cols_ - 1))); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 768 | } |
Tao Bao | e15d7a5 | 2017-09-07 13:38:51 -0700 | [diff] [blame] | 769 | menu_items = static_cast<int>(menu_.size()); |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 770 | show_menu = true; |
| 771 | menu_sel = initial_selection; |
| 772 | update_screen_locked(); |
| 773 | } |
| 774 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | int ScreenRecoveryUI::SelectMenu(int sel) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 778 | pthread_mutex_lock(&updateMutex); |
| 779 | if (show_menu) { |
| 780 | int old_sel = menu_sel; |
| 781 | menu_sel = sel; |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 782 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 783 | // Wrap at top and bottom. |
| 784 | if (menu_sel < 0) menu_sel = menu_items - 1; |
| 785 | if (menu_sel >= menu_items) menu_sel = 0; |
Elliott Hughes | fc06f87 | 2015-03-23 13:45:31 -0700 | [diff] [blame] | 786 | |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 787 | sel = menu_sel; |
| 788 | if (menu_sel != old_sel) update_screen_locked(); |
| 789 | } |
| 790 | pthread_mutex_unlock(&updateMutex); |
| 791 | return sel; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | void ScreenRecoveryUI::EndMenu() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 795 | pthread_mutex_lock(&updateMutex); |
| 796 | if (show_menu && text_rows_ > 0 && text_cols_ > 0) { |
| 797 | show_menu = false; |
| 798 | update_screen_locked(); |
| 799 | } |
| 800 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 803 | bool ScreenRecoveryUI::IsTextVisible() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 804 | pthread_mutex_lock(&updateMutex); |
| 805 | int visible = show_text; |
| 806 | pthread_mutex_unlock(&updateMutex); |
| 807 | return visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 810 | bool ScreenRecoveryUI::WasTextEverVisible() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 811 | pthread_mutex_lock(&updateMutex); |
| 812 | int ever_visible = show_text_ever; |
| 813 | pthread_mutex_unlock(&updateMutex); |
| 814 | return ever_visible; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 817 | void ScreenRecoveryUI::ShowText(bool visible) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 818 | pthread_mutex_lock(&updateMutex); |
| 819 | show_text = visible; |
| 820 | if (show_text) show_text_ever = true; |
| 821 | update_screen_locked(); |
| 822 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 823 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 824 | |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 825 | void ScreenRecoveryUI::Redraw() { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 826 | pthread_mutex_lock(&updateMutex); |
| 827 | update_screen_locked(); |
| 828 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 829 | } |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 830 | |
| 831 | void ScreenRecoveryUI::KeyLongPress(int) { |
Tao Bao | 5d2e3bd | 2017-06-23 22:23:50 -0700 | [diff] [blame] | 832 | // Redraw so that if we're in the menu, the highlight |
| 833 | // will change color to indicate a successful long press. |
| 834 | Redraw(); |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 835 | } |