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