blob: 2a8652ecf5e6cec3902e577f6c38138a192d1807 [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),
78 indeterminate_frames(6),
79 installing_frames(7),
80 install_overlay_offset_x(13),
81 install_overlay_offset_y(190) {
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 Zongker32a0a472011-11-01 11:00:20 -070097 install_overlay_offset_x, install_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];
110 int iconWidth = gr_get_width(surface);
111 int iconHeight = gr_get_height(surface);
112 int iconX = (gr_fb_width() - iconWidth) / 2;
113 int iconY = (gr_fb_height() - iconHeight) / 2;
114 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
115 if (icon == INSTALLING) {
116 draw_install_overlay_locked(installingFrame);
117 }
118 }
119}
120
121// Draw the progress bar (if any) on the screen. Does not flip pages.
122// Should only be called with updateMutex locked.
123void ScreenRecoveryUI::draw_progress_locked()
124{
125 if (currentIcon == INSTALLING) {
126 draw_install_overlay_locked(installingFrame);
127 }
128
129 if (progressBarType != EMPTY) {
130 int iconHeight = gr_get_height(backgroundIcon[INSTALLING]);
131 int width = gr_get_width(progressBarEmpty);
132 int height = gr_get_height(progressBarEmpty);
133
134 int dx = (gr_fb_width() - width)/2;
135 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
136
137 // Erase behind the progress bar (in case this was a progress-only update)
138 gr_color(0, 0, 0, 255);
139 gr_fill(dx, dy, width, height);
140
141 if (progressBarType == DETERMINATE) {
142 float p = progressScopeStart + progress * progressScopeSize;
143 int pos = (int) (p * width);
144
145 if (pos > 0) {
146 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
147 }
148 if (pos < width-1) {
149 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
150 }
151 }
152
153 if (progressBarType == INDETERMINATE) {
154 static int frame = 0;
155 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker32a0a472011-11-01 11:00:20 -0700156 frame = (frame + 1) % indeterminate_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700157 }
158 }
159}
160
161void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
162 if (t[0] != '\0') {
163 gr_text(0, (row+1)*CHAR_HEIGHT-1, t);
164 }
165}
166
167// Redraw everything on the screen. Does not flip pages.
168// Should only be called with updateMutex locked.
169void ScreenRecoveryUI::draw_screen_locked()
170{
171 draw_background_locked(currentIcon);
172 draw_progress_locked();
173
174 if (show_text) {
175 gr_color(0, 0, 0, 160);
176 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
177
178 int i = 0;
179 if (show_menu) {
180 gr_color(64, 96, 255, 255);
181 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
182 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
183
184 for (; i < menu_top + menu_items; ++i) {
185 if (i == menu_top + menu_sel) {
186 gr_color(255, 255, 255, 255);
187 draw_text_line(i, menu[i]);
188 gr_color(64, 96, 255, 255);
189 } else {
190 draw_text_line(i, menu[i]);
191 }
192 }
193 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
194 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
195 ++i;
196 }
197
198 gr_color(255, 255, 0, 255);
199
200 for (; i < text_rows; ++i) {
201 draw_text_line(i, text[(i+text_top) % text_rows]);
202 }
203 }
204}
205
206// Redraw everything on the screen and flip the screen (make it visible).
207// Should only be called with updateMutex locked.
208void ScreenRecoveryUI::update_screen_locked()
209{
210 draw_screen_locked();
211 gr_flip();
212}
213
214// Updates only the progress bar, if possible, otherwise redraws the screen.
215// Should only be called with updateMutex locked.
216void ScreenRecoveryUI::update_progress_locked()
217{
218 if (show_text || !pagesIdentical) {
219 draw_screen_locked(); // Must redraw the whole screen
220 pagesIdentical = true;
221 } else {
222 draw_progress_locked(); // Draw only the progress bar and overlays
223 }
224 gr_flip();
225}
226
227// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700228void* ScreenRecoveryUI::progress_thread(void *cookie) {
229 self->progress_loop();
230 return NULL;
231}
232
233void ScreenRecoveryUI::progress_loop() {
234 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700235 for (;;) {
236 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700237 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700238
239 int redraw = 0;
240
241 // update the installation animation, if active
242 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700243 if (currentIcon == INSTALLING && installing_frames > 0 && !show_text) {
244 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700245 redraw = 1;
246 }
247
248 // update the progress bar animation, if active
249 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700250 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700251 redraw = 1;
252 }
253
254 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700255 int duration = progressScopeDuration;
256 if (progressBarType == DETERMINATE && duration > 0) {
257 double elapsed = now() - progressScopeTime;
Doug Zongker211aebc2011-10-28 15:13:10 -0700258 float progress = 1.0 * elapsed / duration;
259 if (progress > 1.0) progress = 1.0;
260 if (progress > progress) {
261 progress = progress;
262 redraw = 1;
263 }
264 }
265
Doug Zongker32a0a472011-11-01 11:00:20 -0700266 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700267
Doug Zongker32a0a472011-11-01 11:00:20 -0700268 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700269 double end = now();
270 // minimum of 20ms delay between frames
271 double delay = interval - (end-start);
272 if (delay < 0.02) delay = 0.02;
273 usleep((long)(delay * 1000000));
274 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700275}
276
277void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
278 int result = res_create_surface(filename, surface);
279 if (result < 0) {
280 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
281 }
282}
283
284void ScreenRecoveryUI::Init()
285{
286 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700287
288 text_col = text_row = 0;
289 text_rows = gr_fb_height() / CHAR_HEIGHT;
290 if (text_rows > kMaxRows) text_rows = kMaxRows;
291 text_top = 1;
292
293 text_cols = gr_fb_width() / CHAR_WIDTH;
294 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
295
296 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING]);
297 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
298 LoadBitmap("progress_empty", &progressBarEmpty);
299 LoadBitmap("progress_fill", &progressBarFill);
300
301 int i;
302
Doug Zongker32a0a472011-11-01 11:00:20 -0700303 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700304 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700305 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700306 char filename[40];
307 // "indeterminate01.png", "indeterminate02.png", ...
308 sprintf(filename, "indeterminate%02d", i+1);
309 LoadBitmap(filename, progressBarIndeterminate+i);
310 }
311
Doug Zongker32a0a472011-11-01 11:00:20 -0700312 if (installing_frames > 0) {
313 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700315 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700316 char filename[40];
317 // "icon_installing_overlay01.png",
318 // "icon_installing_overlay02.png", ...
319 sprintf(filename, "icon_installing_overlay%02d", i+1);
320 LoadBitmap(filename, installationOverlay+i);
321 }
322
323 // Adjust the offset to account for the positioning of the
324 // base image on the screen.
325 if (backgroundIcon[INSTALLING] != NULL) {
326 gr_surface bg = backgroundIcon[INSTALLING];
Doug Zongker32a0a472011-11-01 11:00:20 -0700327 install_overlay_offset_x += (gr_fb_width() - gr_get_width(bg)) / 2;
328 install_overlay_offset_y += (gr_fb_height() - gr_get_height(bg)) / 2;
Doug Zongker211aebc2011-10-28 15:13:10 -0700329 }
330 } else {
331 installationOverlay = NULL;
332 }
333
334 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700335
336 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700337}
338
339void ScreenRecoveryUI::SetBackground(Icon icon)
340{
341 pthread_mutex_lock(&updateMutex);
342 currentIcon = icon;
343 update_screen_locked();
344 pthread_mutex_unlock(&updateMutex);
345}
346
347void ScreenRecoveryUI::SetProgressType(ProgressType type)
348{
349 pthread_mutex_lock(&updateMutex);
350 if (progressBarType != type) {
351 progressBarType = type;
352 update_progress_locked();
353 }
354 pthread_mutex_unlock(&updateMutex);
355}
356
357void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
358{
359 pthread_mutex_lock(&updateMutex);
360 progressBarType = DETERMINATE;
361 progressScopeStart += progressScopeSize;
362 progressScopeSize = portion;
363 progressScopeTime = now();
364 progressScopeDuration = seconds;
365 progress = 0;
366 update_progress_locked();
367 pthread_mutex_unlock(&updateMutex);
368}
369
370void ScreenRecoveryUI::SetProgress(float fraction)
371{
372 pthread_mutex_lock(&updateMutex);
373 if (fraction < 0.0) fraction = 0.0;
374 if (fraction > 1.0) fraction = 1.0;
375 if (progressBarType == DETERMINATE && fraction > progress) {
376 // Skip updates that aren't visibly different.
377 int width = gr_get_width(progressBarIndeterminate[0]);
378 float scale = width * progressScopeSize;
379 if ((int) (progress * scale) != (int) (fraction * scale)) {
380 progress = fraction;
381 update_progress_locked();
382 }
383 }
384 pthread_mutex_unlock(&updateMutex);
385}
386
387void ScreenRecoveryUI::Print(const char *fmt, ...)
388{
389 char buf[256];
390 va_list ap;
391 va_start(ap, fmt);
392 vsnprintf(buf, 256, fmt, ap);
393 va_end(ap);
394
395 fputs(buf, stdout);
396
397 // This can get called before ui_init(), so be careful.
398 pthread_mutex_lock(&updateMutex);
399 if (text_rows > 0 && text_cols > 0) {
400 char *ptr;
401 for (ptr = buf; *ptr != '\0'; ++ptr) {
402 if (*ptr == '\n' || text_col >= text_cols) {
403 text[text_row][text_col] = '\0';
404 text_col = 0;
405 text_row = (text_row + 1) % text_rows;
406 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
407 }
408 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
409 }
410 text[text_row][text_col] = '\0';
411 update_screen_locked();
412 }
413 pthread_mutex_unlock(&updateMutex);
414}
415
416void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
417 int initial_selection) {
418 int i;
419 pthread_mutex_lock(&updateMutex);
420 if (text_rows > 0 && text_cols > 0) {
421 for (i = 0; i < text_rows; ++i) {
422 if (headers[i] == NULL) break;
423 strncpy(menu[i], headers[i], text_cols-1);
424 menu[i][text_cols-1] = '\0';
425 }
426 menu_top = i;
427 for (; i < text_rows; ++i) {
428 if (items[i-menu_top] == NULL) break;
429 strncpy(menu[i], items[i-menu_top], text_cols-1);
430 menu[i][text_cols-1] = '\0';
431 }
432 menu_items = i - menu_top;
433 show_menu = 1;
434 menu_sel = initial_selection;
435 update_screen_locked();
436 }
437 pthread_mutex_unlock(&updateMutex);
438}
439
440int ScreenRecoveryUI::SelectMenu(int sel) {
441 int old_sel;
442 pthread_mutex_lock(&updateMutex);
443 if (show_menu > 0) {
444 old_sel = menu_sel;
445 menu_sel = sel;
446 if (menu_sel < 0) menu_sel = 0;
447 if (menu_sel >= menu_items) menu_sel = menu_items-1;
448 sel = menu_sel;
449 if (menu_sel != old_sel) update_screen_locked();
450 }
451 pthread_mutex_unlock(&updateMutex);
452 return sel;
453}
454
455void ScreenRecoveryUI::EndMenu() {
456 int i;
457 pthread_mutex_lock(&updateMutex);
458 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
459 show_menu = 0;
460 update_screen_locked();
461 }
462 pthread_mutex_unlock(&updateMutex);
463}
464
465bool ScreenRecoveryUI::IsTextVisible()
466{
467 pthread_mutex_lock(&updateMutex);
468 int visible = show_text;
469 pthread_mutex_unlock(&updateMutex);
470 return visible;
471}
472
473bool ScreenRecoveryUI::WasTextEverVisible()
474{
475 pthread_mutex_lock(&updateMutex);
476 int ever_visible = show_text_ever;
477 pthread_mutex_unlock(&updateMutex);
478 return ever_visible;
479}
480
481void ScreenRecoveryUI::ShowText(bool visible)
482{
483 pthread_mutex_lock(&updateMutex);
484 show_text = visible;
485 if (show_text) show_text_ever = 1;
486 update_screen_locked();
487 pthread_mutex_unlock(&updateMutex);
488}