blob: 1f2471adee3c61cbffe6ce76c07f2a90e06a53a7 [file] [log] [blame]
Doug Zongker211aebc2011-10-28 15:13:10 -07001/*
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 Zongkerdaefc1d2011-10-31 09:34:15 -070032#include "device.h"
Doug Zongker32a0a472011-11-01 11:00:20 -070033#include "minui/minui.h"
34#include "screen_ui.h"
35#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070036
37#define CHAR_WIDTH 10
38#define CHAR_HEIGHT 18
39
Doug Zongker211aebc2011-10-28 15:13:10 -070040// There's only (at most) one of these objects, and global callbacks
41// (for pthread_create, and the input event system) need to find it,
42// so use a global variable.
43static ScreenRecoveryUI* self = NULL;
44
45// Return the current time as a double (including fractions of a second).
46static double now() {
47 struct timeval tv;
48 gettimeofday(&tv, NULL);
49 return tv.tv_sec + tv.tv_usec / 1000000.0;
50}
51
52ScreenRecoveryUI::ScreenRecoveryUI() :
53 currentIcon(NONE),
54 installingFrame(0),
55 progressBarType(EMPTY),
56 progressScopeStart(0),
57 progressScopeSize(0),
58 progress(0),
59 pagesIdentical(false),
60 text_cols(0),
61 text_rows(0),
62 text_col(0),
63 text_row(0),
64 text_top(0),
65 show_text(false),
66 show_text_ever(false),
67 show_menu(false),
68 menu_top(0),
69 menu_items(0),
70 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070071
72 // These values are correct for the default image resources
73 // provided with the android platform. Devices which use
74 // different resources should have a subclass of ScreenRecoveryUI
75 // that overrides Init() to set these values appropriately and
76 // then call the superclass Init().
77 animation_fps(20),
Doug Zongker4f33e552012-08-23 13:16:12 -070078 indeterminate_frames(16),
79 installing_frames(48),
80 install_overlay_offset_x(65),
81 install_overlay_offset_y(106) {
Doug Zongker211aebc2011-10-28 15:13:10 -070082 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070083 self = this;
84}
85
86// Draw the given frame over the installation overlay animation. The
87// background is not cleared or draw with the base icon first; we
88// assume that the frame already contains some other frame of the
89// animation. Does nothing if no overlay animation is defined.
90// Should only be called with updateMutex locked.
91void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
92 if (installationOverlay == NULL) return;
93 gr_surface surface = installationOverlay[frame];
94 int iconWidth = gr_get_width(surface);
95 int iconHeight = gr_get_height(surface);
96 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker02ec6b82012-08-22 17:26:40 -070097 overlay_offset_x, overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -070098}
99
100// Clear the screen and draw the currently selected background icon (if any).
101// Should only be called with updateMutex locked.
102void ScreenRecoveryUI::draw_background_locked(Icon icon)
103{
104 pagesIdentical = false;
105 gr_color(0, 0, 0, 255);
106 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
107
108 if (icon) {
109 gr_surface surface = backgroundIcon[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700110 gr_surface text_surface = backgroundText[icon];
111
Doug Zongker211aebc2011-10-28 15:13:10 -0700112 int iconWidth = gr_get_width(surface);
113 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700114 int textWidth = gr_get_width(text_surface);
115 int textHeight = gr_get_height(text_surface);
116
Doug Zongker211aebc2011-10-28 15:13:10 -0700117 int iconX = (gr_fb_width() - iconWidth) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700118 int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
119
120 int textX = (gr_fb_width() - textWidth) / 2;
121 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
122
Doug Zongker211aebc2011-10-28 15:13:10 -0700123 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700124 if (icon == INSTALLING_UPDATE || icon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700125 draw_install_overlay_locked(installingFrame);
126 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700127
128 gr_color(255, 255, 255, 255);
129 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700130 }
131}
132
133// Draw the progress bar (if any) on the screen. Does not flip pages.
134// Should only be called with updateMutex locked.
135void ScreenRecoveryUI::draw_progress_locked()
136{
Doug Zongker69f4b672012-04-26 14:37:53 -0700137 if (currentIcon == ERROR) return;
138
Doug Zongker02ec6b82012-08-22 17:26:40 -0700139 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700140 draw_install_overlay_locked(installingFrame);
141 }
142
143 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700144 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700145 int width = gr_get_width(progressBarEmpty);
146 int height = gr_get_height(progressBarEmpty);
147
148 int dx = (gr_fb_width() - width)/2;
149 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
150
151 // Erase behind the progress bar (in case this was a progress-only update)
152 gr_color(0, 0, 0, 255);
153 gr_fill(dx, dy, width, height);
154
155 if (progressBarType == DETERMINATE) {
156 float p = progressScopeStart + progress * progressScopeSize;
157 int pos = (int) (p * width);
158
159 if (pos > 0) {
160 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
161 }
162 if (pos < width-1) {
163 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
164 }
165 }
166
167 if (progressBarType == INDETERMINATE) {
168 static int frame = 0;
169 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker32a0a472011-11-01 11:00:20 -0700170 frame = (frame + 1) % indeterminate_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700171 }
172 }
173}
174
175void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
176 if (t[0] != '\0') {
177 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
178 }
179}
180
181// Redraw everything on the screen. Does not flip pages.
182// Should only be called with updateMutex locked.
183void ScreenRecoveryUI::draw_screen_locked()
184{
185 draw_background_locked(currentIcon);
186 draw_progress_locked();
187
188 if (show_text) {
189 gr_color(0, 0, 0, 160);
190 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
191
192 int i = 0;
193 if (show_menu) {
194 gr_color(64, 96, 255, 255);
195 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
196 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
197
198 for (; i < menu_top + menu_items; ++i) {
199 if (i == menu_top + menu_sel) {
200 gr_color(255, 255, 255, 255);
201 draw_text_line(i, menu[i]);
202 gr_color(64, 96, 255, 255);
203 } else {
204 draw_text_line(i, menu[i]);
205 }
206 }
207 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
208 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
209 ++i;
210 }
211
212 gr_color(255, 255, 0, 255);
213
214 for (; i < text_rows; ++i) {
215 draw_text_line(i, text[(i+text_top) % text_rows]);
216 }
217 }
218}
219
220// Redraw everything on the screen and flip the screen (make it visible).
221// Should only be called with updateMutex locked.
222void ScreenRecoveryUI::update_screen_locked()
223{
224 draw_screen_locked();
225 gr_flip();
226}
227
228// Updates only the progress bar, if possible, otherwise redraws the screen.
229// Should only be called with updateMutex locked.
230void ScreenRecoveryUI::update_progress_locked()
231{
232 if (show_text || !pagesIdentical) {
233 draw_screen_locked(); // Must redraw the whole screen
234 pagesIdentical = true;
235 } else {
236 draw_progress_locked(); // Draw only the progress bar and overlays
237 }
238 gr_flip();
239}
240
241// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700242void* ScreenRecoveryUI::progress_thread(void *cookie) {
243 self->progress_loop();
244 return NULL;
245}
246
247void ScreenRecoveryUI::progress_loop() {
248 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700249 for (;;) {
250 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700251 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700252
253 int redraw = 0;
254
255 // update the installation animation, if active
256 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700257 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
258 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700259 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700260 redraw = 1;
261 }
262
263 // update the progress bar animation, if active
264 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700265 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700266 redraw = 1;
267 }
268
269 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700270 int duration = progressScopeDuration;
271 if (progressBarType == DETERMINATE && duration > 0) {
272 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700273 float p = 1.0 * elapsed / duration;
274 if (p > 1.0) p = 1.0;
275 if (p > progress) {
276 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700277 redraw = 1;
278 }
279 }
280
Doug Zongker32a0a472011-11-01 11:00:20 -0700281 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700282
Doug Zongker32a0a472011-11-01 11:00:20 -0700283 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700284 double end = now();
285 // minimum of 20ms delay between frames
286 double delay = interval - (end-start);
287 if (delay < 0.02) delay = 0.02;
288 usleep((long)(delay * 1000000));
289 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700290}
291
292void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
293 int result = res_create_surface(filename, surface);
294 if (result < 0) {
295 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
296 }
297}
298
Doug Zongker02ec6b82012-08-22 17:26:40 -0700299void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
300 int result = res_create_localized_surface(filename, surface);
301 if (result < 0) {
302 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
303 }
304}
305
Doug Zongker211aebc2011-10-28 15:13:10 -0700306void ScreenRecoveryUI::Init()
307{
308 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700309
310 text_col = text_row = 0;
311 text_rows = gr_fb_height() / CHAR_HEIGHT;
312 if (text_rows > kMaxRows) text_rows = kMaxRows;
313 text_top = 1;
314
315 text_cols = gr_fb_width() / CHAR_WIDTH;
316 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
317
Doug Zongker02ec6b82012-08-22 17:26:40 -0700318 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
319 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700320 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700321 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
322
Doug Zongker211aebc2011-10-28 15:13:10 -0700323 LoadBitmap("progress_empty", &progressBarEmpty);
324 LoadBitmap("progress_fill", &progressBarFill);
325
Doug Zongker02ec6b82012-08-22 17:26:40 -0700326 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
327 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
328 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
329 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
330
Doug Zongker211aebc2011-10-28 15:13:10 -0700331 int i;
332
Doug Zongker32a0a472011-11-01 11:00:20 -0700333 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700334 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700335 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700336 char filename[40];
337 // "indeterminate01.png", "indeterminate02.png", ...
338 sprintf(filename, "indeterminate%02d", i+1);
339 LoadBitmap(filename, progressBarIndeterminate+i);
340 }
341
Doug Zongker32a0a472011-11-01 11:00:20 -0700342 if (installing_frames > 0) {
343 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700344 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700345 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700346 char filename[40];
347 // "icon_installing_overlay01.png",
348 // "icon_installing_overlay02.png", ...
349 sprintf(filename, "icon_installing_overlay%02d", i+1);
350 LoadBitmap(filename, installationOverlay+i);
351 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700352 } else {
353 installationOverlay = NULL;
354 }
355
356 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700357
358 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700359}
360
361void ScreenRecoveryUI::SetBackground(Icon icon)
362{
363 pthread_mutex_lock(&updateMutex);
364 currentIcon = icon;
365 update_screen_locked();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700366
367 // Adjust the offset to account for the positioning of the
368 // base image on the screen.
369 if (backgroundIcon[icon] != NULL) {
370 gr_surface bg = backgroundIcon[icon];
371 gr_surface text = backgroundText[icon];
372 overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
373 overlay_offset_y = install_overlay_offset_y +
374 (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
375 }
376
Doug Zongker211aebc2011-10-28 15:13:10 -0700377 pthread_mutex_unlock(&updateMutex);
378}
379
380void ScreenRecoveryUI::SetProgressType(ProgressType type)
381{
382 pthread_mutex_lock(&updateMutex);
383 if (progressBarType != type) {
384 progressBarType = type;
385 update_progress_locked();
386 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700387 progressScopeStart = 0;
388 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700389 pthread_mutex_unlock(&updateMutex);
390}
391
392void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
393{
394 pthread_mutex_lock(&updateMutex);
395 progressBarType = DETERMINATE;
396 progressScopeStart += progressScopeSize;
397 progressScopeSize = portion;
398 progressScopeTime = now();
399 progressScopeDuration = seconds;
400 progress = 0;
401 update_progress_locked();
402 pthread_mutex_unlock(&updateMutex);
403}
404
405void ScreenRecoveryUI::SetProgress(float fraction)
406{
407 pthread_mutex_lock(&updateMutex);
408 if (fraction < 0.0) fraction = 0.0;
409 if (fraction > 1.0) fraction = 1.0;
410 if (progressBarType == DETERMINATE && fraction > progress) {
411 // Skip updates that aren't visibly different.
412 int width = gr_get_width(progressBarIndeterminate[0]);
413 float scale = width * progressScopeSize;
414 if ((int) (progress * scale) != (int) (fraction * scale)) {
415 progress = fraction;
416 update_progress_locked();
417 }
418 }
419 pthread_mutex_unlock(&updateMutex);
420}
421
422void ScreenRecoveryUI::Print(const char *fmt, ...)
423{
424 char buf[256];
425 va_list ap;
426 va_start(ap, fmt);
427 vsnprintf(buf, 256, fmt, ap);
428 va_end(ap);
429
430 fputs(buf, stdout);
431
432 // This can get called before ui_init(), so be careful.
433 pthread_mutex_lock(&updateMutex);
434 if (text_rows > 0 && text_cols > 0) {
435 char *ptr;
436 for (ptr = buf; *ptr != '\0'; ++ptr) {
437 if (*ptr == '\n' || text_col >= text_cols) {
438 text[text_row][text_col] = '\0';
439 text_col = 0;
440 text_row = (text_row + 1) % text_rows;
441 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
442 }
443 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
444 }
445 text[text_row][text_col] = '\0';
446 update_screen_locked();
447 }
448 pthread_mutex_unlock(&updateMutex);
449}
450
451void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
452 int initial_selection) {
453 int i;
454 pthread_mutex_lock(&updateMutex);
455 if (text_rows > 0 && text_cols > 0) {
456 for (i = 0; i < text_rows; ++i) {
457 if (headers[i] == NULL) break;
458 strncpy(menu[i], headers[i], text_cols-1);
459 menu[i][text_cols-1] = '\0';
460 }
461 menu_top = i;
462 for (; i < text_rows; ++i) {
463 if (items[i-menu_top] == NULL) break;
464 strncpy(menu[i], items[i-menu_top], text_cols-1);
465 menu[i][text_cols-1] = '\0';
466 }
467 menu_items = i - menu_top;
468 show_menu = 1;
469 menu_sel = initial_selection;
470 update_screen_locked();
471 }
472 pthread_mutex_unlock(&updateMutex);
473}
474
475int ScreenRecoveryUI::SelectMenu(int sel) {
476 int old_sel;
477 pthread_mutex_lock(&updateMutex);
478 if (show_menu > 0) {
479 old_sel = menu_sel;
480 menu_sel = sel;
481 if (menu_sel < 0) menu_sel = 0;
482 if (menu_sel >= menu_items) menu_sel = menu_items-1;
483 sel = menu_sel;
484 if (menu_sel != old_sel) update_screen_locked();
485 }
486 pthread_mutex_unlock(&updateMutex);
487 return sel;
488}
489
490void ScreenRecoveryUI::EndMenu() {
491 int i;
492 pthread_mutex_lock(&updateMutex);
493 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
494 show_menu = 0;
495 update_screen_locked();
496 }
497 pthread_mutex_unlock(&updateMutex);
498}
499
500bool ScreenRecoveryUI::IsTextVisible()
501{
502 pthread_mutex_lock(&updateMutex);
503 int visible = show_text;
504 pthread_mutex_unlock(&updateMutex);
505 return visible;
506}
507
508bool ScreenRecoveryUI::WasTextEverVisible()
509{
510 pthread_mutex_lock(&updateMutex);
511 int ever_visible = show_text_ever;
512 pthread_mutex_unlock(&updateMutex);
513 return ever_visible;
514}
515
516void ScreenRecoveryUI::ShowText(bool visible)
517{
518 pthread_mutex_lock(&updateMutex);
519 show_text = visible;
520 if (show_text) show_text_ever = 1;
521 update_screen_locked();
522 pthread_mutex_unlock(&updateMutex);
523}