blob: 60a0ad4965478c8ec85b54f03f96f25d0e3c2cf8 [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040036extern "C" {
37#include "minuitwrp/minui.h"
38int twgr_text(int x, int y, const char *s);
39}
Doug Zongker211aebc2011-10-28 15:13:10 -070040
41#define CHAR_WIDTH 10
42#define CHAR_HEIGHT 18
43
Doug Zongker211aebc2011-10-28 15:13:10 -070044// There's only (at most) one of these objects, and global callbacks
45// (for pthread_create, and the input event system) need to find it,
46// so use a global variable.
47static ScreenRecoveryUI* self = NULL;
48
49// Return the current time as a double (including fractions of a second).
50static double now() {
51 struct timeval tv;
52 gettimeofday(&tv, NULL);
53 return tv.tv_sec + tv.tv_usec / 1000000.0;
54}
55
56ScreenRecoveryUI::ScreenRecoveryUI() :
57 currentIcon(NONE),
58 installingFrame(0),
59 progressBarType(EMPTY),
60 progressScopeStart(0),
61 progressScopeSize(0),
62 progress(0),
63 pagesIdentical(false),
64 text_cols(0),
65 text_rows(0),
66 text_col(0),
67 text_row(0),
68 text_top(0),
69 show_text(false),
70 show_text_ever(false),
71 show_menu(false),
72 menu_top(0),
73 menu_items(0),
74 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070075
76 // These values are correct for the default image resources
77 // provided with the android platform. Devices which use
78 // different resources should have a subclass of ScreenRecoveryUI
79 // that overrides Init() to set these values appropriately and
80 // then call the superclass Init().
81 animation_fps(20),
82 indeterminate_frames(6),
83 installing_frames(7),
84 install_overlay_offset_x(13),
85 install_overlay_offset_y(190) {
Doug Zongker211aebc2011-10-28 15:13:10 -070086 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070087 self = this;
88}
89
90// Draw the given frame over the installation overlay animation. The
91// background is not cleared or draw with the base icon first; we
92// assume that the frame already contains some other frame of the
93// animation. Does nothing if no overlay animation is defined.
94// Should only be called with updateMutex locked.
95void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
96 if (installationOverlay == NULL) return;
97 gr_surface surface = installationOverlay[frame];
98 int iconWidth = gr_get_width(surface);
99 int iconHeight = gr_get_height(surface);
100 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker32a0a472011-11-01 11:00:20 -0700101 install_overlay_offset_x, install_overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700102}
103
104// Clear the screen and draw the currently selected background icon (if any).
105// Should only be called with updateMutex locked.
106void ScreenRecoveryUI::draw_background_locked(Icon icon)
107{
108 pagesIdentical = false;
109 gr_color(0, 0, 0, 255);
110 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
111
112 if (icon) {
113 gr_surface surface = backgroundIcon[icon];
114 int iconWidth = gr_get_width(surface);
115 int iconHeight = gr_get_height(surface);
116 int iconX = (gr_fb_width() - iconWidth) / 2;
117 int iconY = (gr_fb_height() - iconHeight) / 2;
118 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
119 if (icon == INSTALLING) {
120 draw_install_overlay_locked(installingFrame);
121 }
122 }
123}
124
125// Draw the progress bar (if any) on the screen. Does not flip pages.
126// Should only be called with updateMutex locked.
127void ScreenRecoveryUI::draw_progress_locked()
128{
Doug Zongker69f4b672012-04-26 14:37:53 -0700129 if (currentIcon == ERROR) return;
130
Doug Zongker211aebc2011-10-28 15:13:10 -0700131 if (currentIcon == INSTALLING) {
132 draw_install_overlay_locked(installingFrame);
133 }
134
135 if (progressBarType != EMPTY) {
136 int iconHeight = gr_get_height(backgroundIcon[INSTALLING]);
137 int width = gr_get_width(progressBarEmpty);
138 int height = gr_get_height(progressBarEmpty);
139
140 int dx = (gr_fb_width() - width)/2;
141 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
142
143 // Erase behind the progress bar (in case this was a progress-only update)
144 gr_color(0, 0, 0, 255);
145 gr_fill(dx, dy, width, height);
146
147 if (progressBarType == DETERMINATE) {
148 float p = progressScopeStart + progress * progressScopeSize;
149 int pos = (int) (p * width);
150
151 if (pos > 0) {
152 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
153 }
154 if (pos < width-1) {
155 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
156 }
157 }
158
159 if (progressBarType == INDETERMINATE) {
160 static int frame = 0;
161 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker32a0a472011-11-01 11:00:20 -0700162 frame = (frame + 1) % indeterminate_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700163 }
164 }
165}
166
167void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
168 if (t[0] != '\0') {
Dees_Troy51a0e822012-09-05 15:24:24 -0400169 twgr_text(0, (row+1)*CHAR_HEIGHT-1, t);
Doug Zongker211aebc2011-10-28 15:13:10 -0700170 }
171}
172
173// Redraw everything on the screen. Does not flip pages.
174// Should only be called with updateMutex locked.
175void ScreenRecoveryUI::draw_screen_locked()
176{
177 draw_background_locked(currentIcon);
178 draw_progress_locked();
179
180 if (show_text) {
181 gr_color(0, 0, 0, 160);
182 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
183
184 int i = 0;
185 if (show_menu) {
186 gr_color(64, 96, 255, 255);
187 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
188 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
189
190 for (; i < menu_top + menu_items; ++i) {
191 if (i == menu_top + menu_sel) {
192 gr_color(255, 255, 255, 255);
193 draw_text_line(i, menu[i]);
194 gr_color(64, 96, 255, 255);
195 } else {
196 draw_text_line(i, menu[i]);
197 }
198 }
199 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
200 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
201 ++i;
202 }
203
204 gr_color(255, 255, 0, 255);
205
206 for (; i < text_rows; ++i) {
207 draw_text_line(i, text[(i+text_top) % text_rows]);
208 }
209 }
210}
211
212// Redraw everything on the screen and flip the screen (make it visible).
213// Should only be called with updateMutex locked.
214void ScreenRecoveryUI::update_screen_locked()
215{
216 draw_screen_locked();
217 gr_flip();
218}
219
220// Updates only the progress bar, if possible, otherwise redraws the screen.
221// Should only be called with updateMutex locked.
222void ScreenRecoveryUI::update_progress_locked()
223{
224 if (show_text || !pagesIdentical) {
225 draw_screen_locked(); // Must redraw the whole screen
226 pagesIdentical = true;
227 } else {
228 draw_progress_locked(); // Draw only the progress bar and overlays
229 }
230 gr_flip();
231}
232
233// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700234void* ScreenRecoveryUI::progress_thread(void *cookie) {
235 self->progress_loop();
236 return NULL;
237}
238
239void ScreenRecoveryUI::progress_loop() {
240 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700241 for (;;) {
242 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700243 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700244
245 int redraw = 0;
246
247 // update the installation animation, if active
248 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700249 if (currentIcon == INSTALLING && installing_frames > 0 && !show_text) {
250 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700251 redraw = 1;
252 }
253
254 // update the progress bar animation, if active
255 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700256 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700257 redraw = 1;
258 }
259
260 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700261 int duration = progressScopeDuration;
262 if (progressBarType == DETERMINATE && duration > 0) {
263 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700264 float p = 1.0 * elapsed / duration;
265 if (p > 1.0) p = 1.0;
266 if (p > progress) {
267 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700268 redraw = 1;
269 }
270 }
271
Doug Zongker32a0a472011-11-01 11:00:20 -0700272 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700273
Doug Zongker32a0a472011-11-01 11:00:20 -0700274 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700275 double end = now();
276 // minimum of 20ms delay between frames
277 double delay = interval - (end-start);
278 if (delay < 0.02) delay = 0.02;
279 usleep((long)(delay * 1000000));
280 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700281}
282
283void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
284 int result = res_create_surface(filename, surface);
285 if (result < 0) {
286 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
287 }
288}
289
290void ScreenRecoveryUI::Init()
291{
292 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700293
294 text_col = text_row = 0;
295 text_rows = gr_fb_height() / CHAR_HEIGHT;
296 if (text_rows > kMaxRows) text_rows = kMaxRows;
297 text_top = 1;
298
299 text_cols = gr_fb_width() / CHAR_WIDTH;
300 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
301
302 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING]);
303 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
304 LoadBitmap("progress_empty", &progressBarEmpty);
305 LoadBitmap("progress_fill", &progressBarFill);
306
307 int i;
308
Doug Zongker32a0a472011-11-01 11:00:20 -0700309 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700310 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700311 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700312 char filename[40];
313 // "indeterminate01.png", "indeterminate02.png", ...
314 sprintf(filename, "indeterminate%02d", i+1);
315 LoadBitmap(filename, progressBarIndeterminate+i);
316 }
317
Doug Zongker32a0a472011-11-01 11:00:20 -0700318 if (installing_frames > 0) {
319 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700320 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700321 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700322 char filename[40];
323 // "icon_installing_overlay01.png",
324 // "icon_installing_overlay02.png", ...
325 sprintf(filename, "icon_installing_overlay%02d", i+1);
326 LoadBitmap(filename, installationOverlay+i);
327 }
328
329 // Adjust the offset to account for the positioning of the
330 // base image on the screen.
331 if (backgroundIcon[INSTALLING] != NULL) {
332 gr_surface bg = backgroundIcon[INSTALLING];
Doug Zongker32a0a472011-11-01 11:00:20 -0700333 install_overlay_offset_x += (gr_fb_width() - gr_get_width(bg)) / 2;
334 install_overlay_offset_y += (gr_fb_height() - gr_get_height(bg)) / 2;
Doug Zongker211aebc2011-10-28 15:13:10 -0700335 }
336 } else {
337 installationOverlay = NULL;
338 }
339
340 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700341
342 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700343}
344
345void ScreenRecoveryUI::SetBackground(Icon icon)
346{
347 pthread_mutex_lock(&updateMutex);
348 currentIcon = icon;
349 update_screen_locked();
350 pthread_mutex_unlock(&updateMutex);
351}
352
353void ScreenRecoveryUI::SetProgressType(ProgressType type)
354{
355 pthread_mutex_lock(&updateMutex);
356 if (progressBarType != type) {
357 progressBarType = type;
358 update_progress_locked();
359 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700360 progressScopeStart = 0;
361 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700362 pthread_mutex_unlock(&updateMutex);
363}
364
365void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
366{
367 pthread_mutex_lock(&updateMutex);
368 progressBarType = DETERMINATE;
369 progressScopeStart += progressScopeSize;
370 progressScopeSize = portion;
371 progressScopeTime = now();
372 progressScopeDuration = seconds;
373 progress = 0;
374 update_progress_locked();
375 pthread_mutex_unlock(&updateMutex);
376}
377
378void ScreenRecoveryUI::SetProgress(float fraction)
379{
380 pthread_mutex_lock(&updateMutex);
381 if (fraction < 0.0) fraction = 0.0;
382 if (fraction > 1.0) fraction = 1.0;
383 if (progressBarType == DETERMINATE && fraction > progress) {
384 // Skip updates that aren't visibly different.
385 int width = gr_get_width(progressBarIndeterminate[0]);
386 float scale = width * progressScopeSize;
387 if ((int) (progress * scale) != (int) (fraction * scale)) {
388 progress = fraction;
389 update_progress_locked();
390 }
391 }
392 pthread_mutex_unlock(&updateMutex);
393}
394
395void ScreenRecoveryUI::Print(const char *fmt, ...)
396{
397 char buf[256];
398 va_list ap;
399 va_start(ap, fmt);
400 vsnprintf(buf, 256, fmt, ap);
401 va_end(ap);
402
403 fputs(buf, stdout);
404
405 // This can get called before ui_init(), so be careful.
406 pthread_mutex_lock(&updateMutex);
407 if (text_rows > 0 && text_cols > 0) {
408 char *ptr;
409 for (ptr = buf; *ptr != '\0'; ++ptr) {
410 if (*ptr == '\n' || text_col >= text_cols) {
411 text[text_row][text_col] = '\0';
412 text_col = 0;
413 text_row = (text_row + 1) % text_rows;
414 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
415 }
416 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
417 }
418 text[text_row][text_col] = '\0';
419 update_screen_locked();
420 }
421 pthread_mutex_unlock(&updateMutex);
422}
423
424void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
425 int initial_selection) {
426 int i;
427 pthread_mutex_lock(&updateMutex);
428 if (text_rows > 0 && text_cols > 0) {
429 for (i = 0; i < text_rows; ++i) {
430 if (headers[i] == NULL) break;
431 strncpy(menu[i], headers[i], text_cols-1);
432 menu[i][text_cols-1] = '\0';
433 }
434 menu_top = i;
435 for (; i < text_rows; ++i) {
436 if (items[i-menu_top] == NULL) break;
437 strncpy(menu[i], items[i-menu_top], text_cols-1);
438 menu[i][text_cols-1] = '\0';
439 }
440 menu_items = i - menu_top;
441 show_menu = 1;
442 menu_sel = initial_selection;
443 update_screen_locked();
444 }
445 pthread_mutex_unlock(&updateMutex);
446}
447
448int ScreenRecoveryUI::SelectMenu(int sel) {
449 int old_sel;
450 pthread_mutex_lock(&updateMutex);
451 if (show_menu > 0) {
452 old_sel = menu_sel;
453 menu_sel = sel;
454 if (menu_sel < 0) menu_sel = 0;
455 if (menu_sel >= menu_items) menu_sel = menu_items-1;
456 sel = menu_sel;
457 if (menu_sel != old_sel) update_screen_locked();
458 }
459 pthread_mutex_unlock(&updateMutex);
460 return sel;
461}
462
463void ScreenRecoveryUI::EndMenu() {
464 int i;
465 pthread_mutex_lock(&updateMutex);
466 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
467 show_menu = 0;
468 update_screen_locked();
469 }
470 pthread_mutex_unlock(&updateMutex);
471}
472
473bool ScreenRecoveryUI::IsTextVisible()
474{
475 pthread_mutex_lock(&updateMutex);
476 int visible = show_text;
477 pthread_mutex_unlock(&updateMutex);
478 return visible;
479}
480
481bool ScreenRecoveryUI::WasTextEverVisible()
482{
483 pthread_mutex_lock(&updateMutex);
484 int ever_visible = show_text_ever;
485 pthread_mutex_unlock(&updateMutex);
486 return ever_visible;
487}
488
489void ScreenRecoveryUI::ShowText(bool visible)
490{
491 pthread_mutex_lock(&updateMutex);
492 show_text = visible;
493 if (show_text) show_text_ever = 1;
494 update_screen_locked();
495 pthread_mutex_unlock(&updateMutex);
496}