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