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