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