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