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