blob: 0b343754743994fa414ab69cbd4b6414d61c13a6 [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),
Doug Zongker52eeea42012-09-04 14:28:25 -070081 install_overlay_offset_y(106),
82 overlay_offset_x(-1),
83 overlay_offset_y(-1) {
Doug Zongker211aebc2011-10-28 15:13:10 -070084 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070085 self = this;
86}
87
88// Draw the given frame over the installation overlay animation. The
89// background is not cleared or draw with the base icon first; we
90// assume that the frame already contains some other frame of the
91// animation. Does nothing if no overlay animation is defined.
92// Should only be called with updateMutex locked.
93void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
Doug Zongker52eeea42012-09-04 14:28:25 -070094 if (installationOverlay == NULL || overlay_offset_x < 0) return;
Doug Zongker211aebc2011-10-28 15:13:10 -070095 gr_surface surface = installationOverlay[frame];
96 int iconWidth = gr_get_width(surface);
97 int iconHeight = gr_get_height(surface);
98 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker02ec6b82012-08-22 17:26:40 -070099 overlay_offset_x, overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700100}
101
102// Clear the screen and draw the currently selected background icon (if any).
103// Should only be called with updateMutex locked.
104void ScreenRecoveryUI::draw_background_locked(Icon icon)
105{
106 pagesIdentical = false;
107 gr_color(0, 0, 0, 255);
108 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
109
110 if (icon) {
111 gr_surface surface = backgroundIcon[icon];
Doug Zongker02ec6b82012-08-22 17:26:40 -0700112 gr_surface text_surface = backgroundText[icon];
113
Doug Zongker211aebc2011-10-28 15:13:10 -0700114 int iconWidth = gr_get_width(surface);
115 int iconHeight = gr_get_height(surface);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700116 int textWidth = gr_get_width(text_surface);
117 int textHeight = gr_get_height(text_surface);
118
Doug Zongker211aebc2011-10-28 15:13:10 -0700119 int iconX = (gr_fb_width() - iconWidth) / 2;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700120 int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
121
122 int textX = (gr_fb_width() - textWidth) / 2;
123 int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
124
Doug Zongker211aebc2011-10-28 15:13:10 -0700125 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700126 if (icon == INSTALLING_UPDATE || icon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700127 draw_install_overlay_locked(installingFrame);
128 }
Doug Zongker02ec6b82012-08-22 17:26:40 -0700129
130 gr_color(255, 255, 255, 255);
131 gr_texticon(textX, textY, text_surface);
Doug Zongker211aebc2011-10-28 15:13:10 -0700132 }
133}
134
135// Draw the progress bar (if any) on the screen. Does not flip pages.
136// Should only be called with updateMutex locked.
137void ScreenRecoveryUI::draw_progress_locked()
138{
Doug Zongker69f4b672012-04-26 14:37:53 -0700139 if (currentIcon == ERROR) return;
140
Doug Zongker02ec6b82012-08-22 17:26:40 -0700141 if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700142 draw_install_overlay_locked(installingFrame);
143 }
144
145 if (progressBarType != EMPTY) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700146 int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
Doug Zongker211aebc2011-10-28 15:13:10 -0700147 int width = gr_get_width(progressBarEmpty);
148 int height = gr_get_height(progressBarEmpty);
149
150 int dx = (gr_fb_width() - width)/2;
151 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
152
153 // Erase behind the progress bar (in case this was a progress-only update)
154 gr_color(0, 0, 0, 255);
155 gr_fill(dx, dy, width, height);
156
157 if (progressBarType == DETERMINATE) {
158 float p = progressScopeStart + progress * progressScopeSize;
159 int pos = (int) (p * width);
160
161 if (pos > 0) {
162 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
163 }
164 if (pos < width-1) {
165 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
166 }
167 }
168
169 if (progressBarType == INDETERMINATE) {
170 static int frame = 0;
171 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker32a0a472011-11-01 11:00:20 -0700172 frame = (frame + 1) % indeterminate_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700173 }
174 }
175}
176
177void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
178 if (t[0] != '\0') {
179 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
180 }
181}
182
183// Redraw everything on the screen. Does not flip pages.
184// Should only be called with updateMutex locked.
185void ScreenRecoveryUI::draw_screen_locked()
186{
187 draw_background_locked(currentIcon);
188 draw_progress_locked();
189
190 if (show_text) {
191 gr_color(0, 0, 0, 160);
192 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
193
194 int i = 0;
195 if (show_menu) {
196 gr_color(64, 96, 255, 255);
197 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
198 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
199
200 for (; i < menu_top + menu_items; ++i) {
201 if (i == menu_top + menu_sel) {
202 gr_color(255, 255, 255, 255);
203 draw_text_line(i, menu[i]);
204 gr_color(64, 96, 255, 255);
205 } else {
206 draw_text_line(i, menu[i]);
207 }
208 }
209 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
210 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
211 ++i;
212 }
213
214 gr_color(255, 255, 0, 255);
215
216 for (; i < text_rows; ++i) {
217 draw_text_line(i, text[(i+text_top) % text_rows]);
218 }
219 }
220}
221
222// Redraw everything on the screen and flip the screen (make it visible).
223// Should only be called with updateMutex locked.
224void ScreenRecoveryUI::update_screen_locked()
225{
226 draw_screen_locked();
227 gr_flip();
228}
229
230// Updates only the progress bar, if possible, otherwise redraws the screen.
231// Should only be called with updateMutex locked.
232void ScreenRecoveryUI::update_progress_locked()
233{
234 if (show_text || !pagesIdentical) {
235 draw_screen_locked(); // Must redraw the whole screen
236 pagesIdentical = true;
237 } else {
238 draw_progress_locked(); // Draw only the progress bar and overlays
239 }
240 gr_flip();
241}
242
243// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700244void* ScreenRecoveryUI::progress_thread(void *cookie) {
245 self->progress_loop();
246 return NULL;
247}
248
249void ScreenRecoveryUI::progress_loop() {
250 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700251 for (;;) {
252 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700253 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700254
255 int redraw = 0;
256
257 // update the installation animation, if active
258 // skip this if we have a text overlay (too expensive to update)
Doug Zongker02ec6b82012-08-22 17:26:40 -0700259 if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
260 installing_frames > 0 && !show_text) {
Doug Zongker32a0a472011-11-01 11:00:20 -0700261 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700262 redraw = 1;
263 }
264
265 // update the progress bar animation, if active
266 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700267 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700268 redraw = 1;
269 }
270
271 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700272 int duration = progressScopeDuration;
273 if (progressBarType == DETERMINATE && duration > 0) {
274 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700275 float p = 1.0 * elapsed / duration;
276 if (p > 1.0) p = 1.0;
277 if (p > progress) {
278 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700279 redraw = 1;
280 }
281 }
282
Doug Zongker32a0a472011-11-01 11:00:20 -0700283 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700284
Doug Zongker32a0a472011-11-01 11:00:20 -0700285 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700286 double end = now();
287 // minimum of 20ms delay between frames
288 double delay = interval - (end-start);
289 if (delay < 0.02) delay = 0.02;
290 usleep((long)(delay * 1000000));
291 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700292}
293
294void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
295 int result = res_create_surface(filename, surface);
296 if (result < 0) {
297 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
298 }
299}
300
Doug Zongker02ec6b82012-08-22 17:26:40 -0700301void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
302 int result = res_create_localized_surface(filename, surface);
303 if (result < 0) {
304 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
305 }
306}
307
Doug Zongker211aebc2011-10-28 15:13:10 -0700308void ScreenRecoveryUI::Init()
309{
310 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700311
312 text_col = text_row = 0;
313 text_rows = gr_fb_height() / CHAR_HEIGHT;
314 if (text_rows > kMaxRows) text_rows = kMaxRows;
315 text_top = 1;
316
317 text_cols = gr_fb_width() / CHAR_WIDTH;
318 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
319
Doug Zongker02ec6b82012-08-22 17:26:40 -0700320 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
321 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
Doug Zongker211aebc2011-10-28 15:13:10 -0700322 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700323 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
324
Doug Zongker211aebc2011-10-28 15:13:10 -0700325 LoadBitmap("progress_empty", &progressBarEmpty);
326 LoadBitmap("progress_fill", &progressBarFill);
327
Doug Zongker02ec6b82012-08-22 17:26:40 -0700328 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
329 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
330 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
331 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
332
Doug Zongker211aebc2011-10-28 15:13:10 -0700333 int i;
334
Doug Zongker32a0a472011-11-01 11:00:20 -0700335 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700336 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700337 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700338 char filename[40];
339 // "indeterminate01.png", "indeterminate02.png", ...
340 sprintf(filename, "indeterminate%02d", i+1);
341 LoadBitmap(filename, progressBarIndeterminate+i);
342 }
343
Doug Zongker32a0a472011-11-01 11:00:20 -0700344 if (installing_frames > 0) {
345 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700346 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700347 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700348 char filename[40];
349 // "icon_installing_overlay01.png",
350 // "icon_installing_overlay02.png", ...
351 sprintf(filename, "icon_installing_overlay%02d", i+1);
352 LoadBitmap(filename, installationOverlay+i);
353 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700354 } else {
355 installationOverlay = NULL;
356 }
357
358 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700359
360 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700361}
362
363void ScreenRecoveryUI::SetBackground(Icon icon)
364{
365 pthread_mutex_lock(&updateMutex);
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 Zongker52eeea42012-09-04 14:28:25 -0700377 currentIcon = icon;
378 update_screen_locked();
379
Doug Zongker211aebc2011-10-28 15:13:10 -0700380 pthread_mutex_unlock(&updateMutex);
381}
382
383void ScreenRecoveryUI::SetProgressType(ProgressType type)
384{
385 pthread_mutex_lock(&updateMutex);
386 if (progressBarType != type) {
387 progressBarType = type;
388 update_progress_locked();
389 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700390 progressScopeStart = 0;
391 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700392 pthread_mutex_unlock(&updateMutex);
393}
394
395void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
396{
397 pthread_mutex_lock(&updateMutex);
398 progressBarType = DETERMINATE;
399 progressScopeStart += progressScopeSize;
400 progressScopeSize = portion;
401 progressScopeTime = now();
402 progressScopeDuration = seconds;
403 progress = 0;
404 update_progress_locked();
405 pthread_mutex_unlock(&updateMutex);
406}
407
408void ScreenRecoveryUI::SetProgress(float fraction)
409{
410 pthread_mutex_lock(&updateMutex);
411 if (fraction < 0.0) fraction = 0.0;
412 if (fraction > 1.0) fraction = 1.0;
413 if (progressBarType == DETERMINATE && fraction > progress) {
414 // Skip updates that aren't visibly different.
415 int width = gr_get_width(progressBarIndeterminate[0]);
416 float scale = width * progressScopeSize;
417 if ((int) (progress * scale) != (int) (fraction * scale)) {
418 progress = fraction;
419 update_progress_locked();
420 }
421 }
422 pthread_mutex_unlock(&updateMutex);
423}
424
425void ScreenRecoveryUI::Print(const char *fmt, ...)
426{
427 char buf[256];
428 va_list ap;
429 va_start(ap, fmt);
430 vsnprintf(buf, 256, fmt, ap);
431 va_end(ap);
432
433 fputs(buf, stdout);
434
435 // This can get called before ui_init(), so be careful.
436 pthread_mutex_lock(&updateMutex);
437 if (text_rows > 0 && text_cols > 0) {
438 char *ptr;
439 for (ptr = buf; *ptr != '\0'; ++ptr) {
440 if (*ptr == '\n' || text_col >= text_cols) {
441 text[text_row][text_col] = '\0';
442 text_col = 0;
443 text_row = (text_row + 1) % text_rows;
444 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
445 }
446 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
447 }
448 text[text_row][text_col] = '\0';
449 update_screen_locked();
450 }
451 pthread_mutex_unlock(&updateMutex);
452}
453
454void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
455 int initial_selection) {
456 int i;
457 pthread_mutex_lock(&updateMutex);
458 if (text_rows > 0 && text_cols > 0) {
459 for (i = 0; i < text_rows; ++i) {
460 if (headers[i] == NULL) break;
461 strncpy(menu[i], headers[i], text_cols-1);
462 menu[i][text_cols-1] = '\0';
463 }
464 menu_top = i;
465 for (; i < text_rows; ++i) {
466 if (items[i-menu_top] == NULL) break;
467 strncpy(menu[i], items[i-menu_top], text_cols-1);
468 menu[i][text_cols-1] = '\0';
469 }
470 menu_items = i - menu_top;
471 show_menu = 1;
472 menu_sel = initial_selection;
473 update_screen_locked();
474 }
475 pthread_mutex_unlock(&updateMutex);
476}
477
478int ScreenRecoveryUI::SelectMenu(int sel) {
479 int old_sel;
480 pthread_mutex_lock(&updateMutex);
481 if (show_menu > 0) {
482 old_sel = menu_sel;
483 menu_sel = sel;
484 if (menu_sel < 0) menu_sel = 0;
485 if (menu_sel >= menu_items) menu_sel = menu_items-1;
486 sel = menu_sel;
487 if (menu_sel != old_sel) update_screen_locked();
488 }
489 pthread_mutex_unlock(&updateMutex);
490 return sel;
491}
492
493void ScreenRecoveryUI::EndMenu() {
494 int i;
495 pthread_mutex_lock(&updateMutex);
496 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
497 show_menu = 0;
498 update_screen_locked();
499 }
500 pthread_mutex_unlock(&updateMutex);
501}
502
503bool ScreenRecoveryUI::IsTextVisible()
504{
505 pthread_mutex_lock(&updateMutex);
506 int visible = show_text;
507 pthread_mutex_unlock(&updateMutex);
508 return visible;
509}
510
511bool ScreenRecoveryUI::WasTextEverVisible()
512{
513 pthread_mutex_lock(&updateMutex);
514 int ever_visible = show_text_ever;
515 pthread_mutex_unlock(&updateMutex);
516 return ever_visible;
517}
518
519void ScreenRecoveryUI::ShowText(bool visible)
520{
521 pthread_mutex_lock(&updateMutex);
522 show_text = visible;
523 if (show_text) show_text_ever = 1;
524 update_screen_locked();
525 pthread_mutex_unlock(&updateMutex);
526}