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 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include "common.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 32 | #include "device.h" |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 33 | #include "minui/minui.h" |
| 34 | #include "screen_ui.h" |
| 35 | #include "ui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | extern "C" { |
| 37 | #include "minuitwrp/minui.h" |
| 38 | int twgr_text(int x, int y, const char *s); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 39 | #include "gui/gui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 41 | #include "data.hpp" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 42 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 43 | static int char_width; |
| 44 | static int char_height; |
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 | // There's only (at most) one of these objects, and global callbacks |
| 47 | // (for pthread_create, and the input event system) need to find it, |
| 48 | // so use a global variable. |
| 49 | static ScreenRecoveryUI* self = NULL; |
| 50 | |
| 51 | // Return the current time as a double (including fractions of a second). |
| 52 | static double now() { |
| 53 | struct timeval tv; |
| 54 | gettimeofday(&tv, NULL); |
| 55 | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| 56 | } |
| 57 | |
| 58 | ScreenRecoveryUI::ScreenRecoveryUI() : |
| 59 | currentIcon(NONE), |
| 60 | installingFrame(0), |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 61 | rtl_locale(false), |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 62 | progressBarType(EMPTY), |
| 63 | progressScopeStart(0), |
| 64 | progressScopeSize(0), |
| 65 | progress(0), |
| 66 | pagesIdentical(false), |
| 67 | text_cols(0), |
| 68 | text_rows(0), |
| 69 | text_col(0), |
| 70 | text_row(0), |
| 71 | text_top(0), |
| 72 | show_text(false), |
| 73 | show_text_ever(false), |
| 74 | show_menu(false), |
| 75 | menu_top(0), |
| 76 | menu_items(0), |
| 77 | menu_sel(0), |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 78 | |
| 79 | // These values are correct for the default image resources |
| 80 | // provided with the android platform. Devices which use |
| 81 | // different resources should have a subclass of ScreenRecoveryUI |
| 82 | // that overrides Init() to set these values appropriately and |
| 83 | // then call the superclass Init(). |
| 84 | animation_fps(20), |
| 85 | indeterminate_frames(6), |
| 86 | installing_frames(7), |
| 87 | install_overlay_offset_x(13), |
Doug Zongker | 8347cb2 | 2012-10-08 08:38:16 -0700 | [diff] [blame] | 88 | install_overlay_offset_y(190), |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 89 | overlay_offset_x(-1), |
| 90 | overlay_offset_y(-1) { |
yetta_wu | 2f6877a | 2013-06-25 15:03:11 +0800 | [diff] [blame] | 91 | |
| 92 | for (int i = 0; i < 5; i++) |
| 93 | backgroundIcon[i] = NULL; |
| 94 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 95 | pthread_mutex_init(&updateMutex, NULL); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 96 | self = this; |
| 97 | } |
| 98 | |
| 99 | // Draw the given frame over the installation overlay animation. The |
| 100 | // background is not cleared or draw with the base icon first; we |
| 101 | // assume that the frame already contains some other frame of the |
| 102 | // animation. Does nothing if no overlay animation is defined. |
| 103 | // Should only be called with updateMutex locked. |
| 104 | void ScreenRecoveryUI::draw_install_overlay_locked(int frame) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 105 | if (installationOverlay == NULL || overlay_offset_x < 0) return; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 106 | gr_surface surface = installationOverlay[frame]; |
| 107 | int iconWidth = gr_get_width(surface); |
| 108 | int iconHeight = gr_get_height(surface); |
| 109 | gr_blit(surface, 0, 0, iconWidth, iconHeight, |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 110 | overlay_offset_x, overlay_offset_y); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Clear the screen and draw the currently selected background icon (if any). |
| 114 | // Should only be called with updateMutex locked. |
| 115 | void ScreenRecoveryUI::draw_background_locked(Icon icon) |
| 116 | { |
| 117 | pagesIdentical = false; |
| 118 | gr_color(0, 0, 0, 255); |
| 119 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 120 | |
| 121 | if (icon) { |
| 122 | gr_surface surface = backgroundIcon[icon]; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 123 | gr_surface text_surface = backgroundText[icon]; |
| 124 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 125 | int iconWidth = gr_get_width(surface); |
| 126 | int iconHeight = gr_get_height(surface); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 127 | int textWidth = gr_get_width(text_surface); |
| 128 | int textHeight = gr_get_height(text_surface); |
| 129 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 130 | int iconX = (gr_fb_width() - iconWidth) / 2; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 131 | int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2; |
| 132 | |
| 133 | int textX = (gr_fb_width() - textWidth) / 2; |
| 134 | int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40; |
| 135 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 136 | gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 137 | if (icon == INSTALLING_UPDATE || icon == ERASING) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 138 | draw_install_overlay_locked(installingFrame); |
| 139 | } |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 140 | |
| 141 | gr_color(255, 255, 255, 255); |
| 142 | gr_texticon(textX, textY, text_surface); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | // Draw the progress bar (if any) on the screen. Does not flip pages. |
| 147 | // Should only be called with updateMutex locked. |
| 148 | void ScreenRecoveryUI::draw_progress_locked() |
| 149 | { |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 150 | if (currentIcon == ERROR) return; |
| 151 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 152 | if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 153 | draw_install_overlay_locked(installingFrame); |
| 154 | } |
| 155 | |
| 156 | if (progressBarType != EMPTY) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 157 | int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 158 | int width = gr_get_width(progressBarEmpty); |
| 159 | int height = gr_get_height(progressBarEmpty); |
| 160 | |
| 161 | int dx = (gr_fb_width() - width)/2; |
| 162 | int dy = (3*gr_fb_height() + iconHeight - 2*height)/4; |
| 163 | |
| 164 | // Erase behind the progress bar (in case this was a progress-only update) |
| 165 | gr_color(0, 0, 0, 255); |
| 166 | gr_fill(dx, dy, width, height); |
| 167 | |
| 168 | if (progressBarType == DETERMINATE) { |
| 169 | float p = progressScopeStart + progress * progressScopeSize; |
| 170 | int pos = (int) (p * width); |
| 171 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 172 | if (rtl_locale) { |
| 173 | // Fill the progress bar from right to left. |
| 174 | if (pos > 0) { |
| 175 | gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy); |
| 176 | } |
| 177 | if (pos < width-1) { |
| 178 | gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy); |
| 179 | } |
| 180 | } else { |
| 181 | // Fill the progress bar from left to right. |
| 182 | if (pos > 0) { |
| 183 | gr_blit(progressBarFill, 0, 0, pos, height, dx, dy); |
| 184 | } |
| 185 | if (pos < width-1) { |
| 186 | gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy); |
| 187 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | if (progressBarType == INDETERMINATE) { |
| 192 | static int frame = 0; |
| 193 | gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy); |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 194 | // in RTL locales, we run the animation backwards, which |
| 195 | // makes the spinner spin the other way. |
| 196 | if (rtl_locale) { |
| 197 | frame = (frame + indeterminate_frames - 1) % indeterminate_frames; |
| 198 | } else { |
| 199 | frame = (frame + 1) % indeterminate_frames; |
| 200 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 205 | void ScreenRecoveryUI::SetColor(UIElement e) { |
| 206 | switch (e) { |
| 207 | case HEADER: |
| 208 | gr_color(247, 0, 6, 255); |
| 209 | break; |
| 210 | case MENU: |
| 211 | case MENU_SEL_BG: |
| 212 | gr_color(0, 106, 157, 255); |
| 213 | break; |
| 214 | case MENU_SEL_FG: |
| 215 | gr_color(255, 255, 255, 255); |
| 216 | break; |
| 217 | case LOG: |
| 218 | gr_color(249, 194, 0, 255); |
| 219 | break; |
| 220 | case TEXT_FILL: |
| 221 | gr_color(0, 0, 0, 160); |
| 222 | break; |
| 223 | default: |
| 224 | gr_color(255, 255, 255, 255); |
| 225 | break; |
| 226 | } |
| 227 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 228 | |
| 229 | // Redraw everything on the screen. Does not flip pages. |
| 230 | // Should only be called with updateMutex locked. |
| 231 | void ScreenRecoveryUI::draw_screen_locked() |
| 232 | { |
| 233 | draw_background_locked(currentIcon); |
| 234 | draw_progress_locked(); |
| 235 | |
| 236 | if (show_text) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 237 | SetColor(TEXT_FILL); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 238 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 239 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 240 | int y = 0; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 241 | int i = 0; |
| 242 | if (show_menu) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 243 | SetColor(HEADER); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 244 | |
| 245 | for (; i < menu_top + menu_items; ++i) { |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 246 | if (i == menu_top) SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 247 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 248 | if (i == menu_top + menu_sel) { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 249 | // draw the highlight bar |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 250 | SetColor(MENU_SEL_BG); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 251 | gr_fill(0, y-2, gr_fb_width(), y+char_height+2); |
| 252 | // white text of selected item |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 253 | SetColor(MENU_SEL_FG); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 254 | if (menu[i][0]) gr_text(4, y, menu[i], 1); |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 255 | SetColor(MENU); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 256 | } else { |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 257 | if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 258 | } |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 259 | y += char_height+4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 260 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 261 | SetColor(MENU); |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 262 | y += 4; |
| 263 | gr_fill(0, y, gr_fb_width(), y+2); |
| 264 | y += 4; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 265 | ++i; |
| 266 | } |
| 267 | |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 268 | SetColor(LOG); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 269 | |
Doug Zongker | 6fd59ac | 2013-03-06 15:01:11 -0800 | [diff] [blame] | 270 | // display from the bottom up, until we hit the top of the |
| 271 | // screen, the bottom of the menu, or we've displayed the |
| 272 | // entire text buffer. |
| 273 | int ty; |
| 274 | int row = (text_top+text_rows-1) % text_rows; |
| 275 | for (int ty = gr_fb_height() - char_height, count = 0; |
| 276 | ty > y+2 && count < text_rows; |
| 277 | ty -= char_height, ++count) { |
| 278 | gr_text(4, ty, text[row], 0); |
| 279 | --row; |
| 280 | if (row < 0) row = text_rows-1; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Redraw everything on the screen and flip the screen (make it visible). |
| 286 | // Should only be called with updateMutex locked. |
| 287 | void ScreenRecoveryUI::update_screen_locked() |
| 288 | { |
| 289 | draw_screen_locked(); |
| 290 | gr_flip(); |
| 291 | } |
| 292 | |
| 293 | // Updates only the progress bar, if possible, otherwise redraws the screen. |
| 294 | // Should only be called with updateMutex locked. |
| 295 | void ScreenRecoveryUI::update_progress_locked() |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 296 | {return; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 297 | if (show_text || !pagesIdentical) { |
| 298 | draw_screen_locked(); // Must redraw the whole screen |
| 299 | pagesIdentical = true; |
| 300 | } else { |
| 301 | draw_progress_locked(); // Draw only the progress bar and overlays |
| 302 | } |
| 303 | gr_flip(); |
| 304 | } |
| 305 | |
| 306 | // Keeps the progress bar updated, even when the process is otherwise busy. |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 307 | void* ScreenRecoveryUI::progress_thread(void *cookie) { |
| 308 | self->progress_loop(); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | void ScreenRecoveryUI::progress_loop() { |
| 313 | double interval = 1.0 / animation_fps; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 314 | for (;;) { |
| 315 | double start = now(); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 316 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 317 | |
| 318 | int redraw = 0; |
| 319 | |
| 320 | // update the installation animation, if active |
| 321 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 322 | if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) && |
| 323 | installing_frames > 0 && !show_text) { |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 324 | installingFrame = (installingFrame + 1) % installing_frames; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 325 | redraw = 1; |
| 326 | } |
| 327 | |
| 328 | // update the progress bar animation, if active |
| 329 | // skip this if we have a text overlay (too expensive to update) |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 330 | if (progressBarType == INDETERMINATE && !show_text) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 331 | redraw = 1; |
| 332 | } |
| 333 | |
| 334 | // move the progress bar forward on timed intervals, if configured |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 335 | int duration = progressScopeDuration; |
| 336 | if (progressBarType == DETERMINATE && duration > 0) { |
| 337 | double elapsed = now() - progressScopeTime; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 338 | float p = 1.0 * elapsed / duration; |
| 339 | if (p > 1.0) p = 1.0; |
| 340 | if (p > progress) { |
| 341 | progress = p; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 342 | redraw = 1; |
| 343 | } |
| 344 | } |
| 345 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 346 | if (redraw) update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 347 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 348 | pthread_mutex_unlock(&updateMutex); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 349 | double end = now(); |
| 350 | // minimum of 20ms delay between frames |
| 351 | double delay = interval - (end-start); |
| 352 | if (delay < 0.02) delay = 0.02; |
| 353 | usleep((long)(delay * 1000000)); |
| 354 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) { |
| 358 | int result = res_create_surface(filename, surface); |
| 359 | if (result < 0) { |
| 360 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 361 | } |
| 362 | } |
| 363 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 364 | void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) { |
| 365 | int result = res_create_localized_surface(filename, surface); |
| 366 | if (result < 0) { |
| 367 | LOGE("missing bitmap %s\n(Code %d)\n", filename, result); |
| 368 | } |
| 369 | } |
| 370 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 371 | void ScreenRecoveryUI::Init() |
| 372 | { |
| 373 | gr_init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 374 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 375 | gr_font_size(&char_width, &char_height); |
| 376 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 377 | text_col = text_row = 0; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 378 | text_rows = gr_fb_height() / char_height; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 379 | if (text_rows > kMaxRows) text_rows = kMaxRows; |
| 380 | text_top = 1; |
| 381 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 382 | text_cols = gr_fb_width() / char_width; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 383 | if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; |
| 384 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 385 | LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]); |
| 386 | backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 387 | LoadBitmap("icon_error", &backgroundIcon[ERROR]); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 388 | backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; |
| 389 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 390 | LoadBitmap("progress_empty", &progressBarEmpty); |
| 391 | LoadBitmap("progress_fill", &progressBarFill); |
| 392 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 393 | LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); |
| 394 | LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); |
| 395 | LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); |
| 396 | LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); |
| 397 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 398 | int i; |
| 399 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 400 | progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames * |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 401 | sizeof(gr_surface)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 402 | for (i = 0; i < indeterminate_frames; ++i) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 403 | char filename[40]; |
| 404 | // "indeterminate01.png", "indeterminate02.png", ... |
| 405 | sprintf(filename, "indeterminate%02d", i+1); |
| 406 | LoadBitmap(filename, progressBarIndeterminate+i); |
| 407 | } |
| 408 | |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 409 | if (installing_frames > 0) { |
| 410 | installationOverlay = (gr_surface*)malloc(installing_frames * |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 411 | sizeof(gr_surface)); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 412 | for (i = 0; i < installing_frames; ++i) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 413 | char filename[40]; |
| 414 | // "icon_installing_overlay01.png", |
| 415 | // "icon_installing_overlay02.png", ... |
| 416 | sprintf(filename, "icon_installing_overlay%02d", i+1); |
| 417 | LoadBitmap(filename, installationOverlay+i); |
| 418 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 419 | } else { |
| 420 | installationOverlay = NULL; |
| 421 | } |
| 422 | |
| 423 | pthread_create(&progress_t, NULL, progress_thread, NULL); |
Doug Zongker | 32a0a47 | 2011-11-01 11:00:20 -0700 | [diff] [blame] | 424 | |
| 425 | RecoveryUI::Init(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 428 | void ScreenRecoveryUI::SetLocale(const char* locale) { |
| 429 | if (locale) { |
| 430 | char* lang = strdup(locale); |
| 431 | for (char* p = lang; *p; ++p) { |
| 432 | if (*p == '_') { |
| 433 | *p = '\0'; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // A bit cheesy: keep an explicit list of supported languages |
| 439 | // that are RTL. |
| 440 | if (strcmp(lang, "ar") == 0 || // Arabic |
| 441 | strcmp(lang, "fa") == 0 || // Persian (Farsi) |
| 442 | strcmp(lang, "he") == 0 || // Hebrew (new language code) |
Doug Zongker | b66cb69 | 2012-09-18 14:52:18 -0700 | [diff] [blame] | 443 | strcmp(lang, "iw") == 0 || // Hebrew (old language code) |
| 444 | strcmp(lang, "ur") == 0) { // Urdu |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame] | 445 | rtl_locale = true; |
| 446 | } |
| 447 | free(lang); |
| 448 | } |
| 449 | } |
| 450 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 451 | void ScreenRecoveryUI::SetBackground(Icon icon) |
| 452 | { |
| 453 | pthread_mutex_lock(&updateMutex); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 454 | |
| 455 | // Adjust the offset to account for the positioning of the |
| 456 | // base image on the screen. |
| 457 | if (backgroundIcon[icon] != NULL) { |
| 458 | gr_surface bg = backgroundIcon[icon]; |
| 459 | gr_surface text = backgroundText[icon]; |
| 460 | overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2; |
| 461 | overlay_offset_y = install_overlay_offset_y + |
| 462 | (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2; |
| 463 | } |
| 464 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 465 | currentIcon = icon; |
| 466 | update_screen_locked(); |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 467 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 468 | pthread_mutex_unlock(&updateMutex); |
| 469 | } |
| 470 | |
| 471 | void ScreenRecoveryUI::SetProgressType(ProgressType type) |
| 472 | { |
| 473 | pthread_mutex_lock(&updateMutex); |
| 474 | if (progressBarType != type) { |
| 475 | progressBarType = type; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 476 | } |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 477 | progressScopeStart = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 478 | progressScopeSize = 0; |
Doug Zongker | 69f4b67 | 2012-04-26 14:37:53 -0700 | [diff] [blame] | 479 | progress = 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 480 | update_progress_locked(); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 481 | pthread_mutex_unlock(&updateMutex); |
| 482 | } |
| 483 | |
| 484 | void ScreenRecoveryUI::ShowProgress(float portion, float seconds) |
| 485 | { |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 486 | DataManager::SetValue("ui_progress_portion", (float)(portion * 100.0)); |
| 487 | DataManager::SetValue("ui_progress_frames", seconds * 30); |
| 488 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 489 | pthread_mutex_lock(&updateMutex); |
| 490 | progressBarType = DETERMINATE; |
| 491 | progressScopeStart += progressScopeSize; |
| 492 | progressScopeSize = portion; |
| 493 | progressScopeTime = now(); |
| 494 | progressScopeDuration = seconds; |
| 495 | progress = 0; |
| 496 | update_progress_locked(); |
| 497 | pthread_mutex_unlock(&updateMutex); |
| 498 | } |
| 499 | |
| 500 | void ScreenRecoveryUI::SetProgress(float fraction) |
| 501 | { |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 502 | DataManager::SetValue("ui_progress", (float) (fraction * 100.0)); return; |
| 503 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 504 | pthread_mutex_lock(&updateMutex); |
| 505 | if (fraction < 0.0) fraction = 0.0; |
| 506 | if (fraction > 1.0) fraction = 1.0; |
| 507 | if (progressBarType == DETERMINATE && fraction > progress) { |
| 508 | // Skip updates that aren't visibly different. |
| 509 | int width = gr_get_width(progressBarIndeterminate[0]); |
| 510 | float scale = width * progressScopeSize; |
| 511 | if ((int) (progress * scale) != (int) (fraction * scale)) { |
| 512 | progress = fraction; |
| 513 | update_progress_locked(); |
| 514 | } |
| 515 | } |
| 516 | pthread_mutex_unlock(&updateMutex); |
| 517 | } |
| 518 | |
| 519 | void ScreenRecoveryUI::Print(const char *fmt, ...) |
| 520 | { |
| 521 | char buf[256]; |
| 522 | va_list ap; |
| 523 | va_start(ap, fmt); |
| 524 | vsnprintf(buf, 256, fmt, ap); |
| 525 | va_end(ap); |
| 526 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 527 | gui_print("%s", buf); |
| 528 | return; |
| 529 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 530 | fputs(buf, stdout); |
| 531 | |
| 532 | // This can get called before ui_init(), so be careful. |
| 533 | pthread_mutex_lock(&updateMutex); |
| 534 | if (text_rows > 0 && text_cols > 0) { |
| 535 | char *ptr; |
| 536 | for (ptr = buf; *ptr != '\0'; ++ptr) { |
| 537 | if (*ptr == '\n' || text_col >= text_cols) { |
| 538 | text[text_row][text_col] = '\0'; |
| 539 | text_col = 0; |
| 540 | text_row = (text_row + 1) % text_rows; |
| 541 | if (text_row == text_top) text_top = (text_top + 1) % text_rows; |
| 542 | } |
| 543 | if (*ptr != '\n') text[text_row][text_col++] = *ptr; |
| 544 | } |
| 545 | text[text_row][text_col] = '\0'; |
| 546 | update_screen_locked(); |
| 547 | } |
| 548 | pthread_mutex_unlock(&updateMutex); |
| 549 | } |
| 550 | |
| 551 | void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, |
| 552 | int initial_selection) { |
| 553 | int i; |
| 554 | pthread_mutex_lock(&updateMutex); |
| 555 | if (text_rows > 0 && text_cols > 0) { |
| 556 | for (i = 0; i < text_rows; ++i) { |
| 557 | if (headers[i] == NULL) break; |
| 558 | strncpy(menu[i], headers[i], text_cols-1); |
| 559 | menu[i][text_cols-1] = '\0'; |
| 560 | } |
| 561 | menu_top = i; |
| 562 | for (; i < text_rows; ++i) { |
| 563 | if (items[i-menu_top] == NULL) break; |
| 564 | strncpy(menu[i], items[i-menu_top], text_cols-1); |
| 565 | menu[i][text_cols-1] = '\0'; |
| 566 | } |
| 567 | menu_items = i - menu_top; |
| 568 | show_menu = 1; |
| 569 | menu_sel = initial_selection; |
| 570 | update_screen_locked(); |
| 571 | } |
| 572 | pthread_mutex_unlock(&updateMutex); |
| 573 | } |
| 574 | |
| 575 | int ScreenRecoveryUI::SelectMenu(int sel) { |
| 576 | int old_sel; |
| 577 | pthread_mutex_lock(&updateMutex); |
| 578 | if (show_menu > 0) { |
| 579 | old_sel = menu_sel; |
| 580 | menu_sel = sel; |
| 581 | if (menu_sel < 0) menu_sel = 0; |
| 582 | if (menu_sel >= menu_items) menu_sel = menu_items-1; |
| 583 | sel = menu_sel; |
| 584 | if (menu_sel != old_sel) update_screen_locked(); |
| 585 | } |
| 586 | pthread_mutex_unlock(&updateMutex); |
| 587 | return sel; |
| 588 | } |
| 589 | |
| 590 | void ScreenRecoveryUI::EndMenu() { |
| 591 | int i; |
| 592 | pthread_mutex_lock(&updateMutex); |
| 593 | if (show_menu > 0 && text_rows > 0 && text_cols > 0) { |
| 594 | show_menu = 0; |
| 595 | update_screen_locked(); |
| 596 | } |
| 597 | pthread_mutex_unlock(&updateMutex); |
| 598 | } |
| 599 | |
| 600 | bool ScreenRecoveryUI::IsTextVisible() |
| 601 | { |
| 602 | pthread_mutex_lock(&updateMutex); |
| 603 | int visible = show_text; |
| 604 | pthread_mutex_unlock(&updateMutex); |
| 605 | return visible; |
| 606 | } |
| 607 | |
| 608 | bool ScreenRecoveryUI::WasTextEverVisible() |
| 609 | { |
| 610 | pthread_mutex_lock(&updateMutex); |
| 611 | int ever_visible = show_text_ever; |
| 612 | pthread_mutex_unlock(&updateMutex); |
| 613 | return ever_visible; |
| 614 | } |
| 615 | |
| 616 | void ScreenRecoveryUI::ShowText(bool visible) |
| 617 | { |
| 618 | pthread_mutex_lock(&updateMutex); |
| 619 | show_text = visible; |
| 620 | if (show_text) show_text_ever = 1; |
| 621 | update_screen_locked(); |
| 622 | pthread_mutex_unlock(&updateMutex); |
| 623 | } |
Doug Zongker | c0441d1 | 2013-07-31 11:28:24 -0700 | [diff] [blame] | 624 | |
| 625 | void ScreenRecoveryUI::Redraw() |
| 626 | { |
| 627 | pthread_mutex_lock(&updateMutex); |
| 628 | update_screen_locked(); |
| 629 | pthread_mutex_unlock(&updateMutex); |
| 630 | } |