blob: 4441f7abcfe0f406500839366fc9b78aa538e490 [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);
Dees_Troy32c8eb82012-09-11 15:28:06 -040039#include "gui/gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
Dees_Troy32c8eb82012-09-11 15:28:06 -040041#include "data.hpp"
Doug Zongker211aebc2011-10-28 15:13:10 -070042
43#define CHAR_WIDTH 10
44#define CHAR_HEIGHT 18
45
Doug Zongker211aebc2011-10-28 15:13:10 -070046// 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.
49static ScreenRecoveryUI* self = NULL;
50
51// Return the current time as a double (including fractions of a second).
52static double now() {
53 struct timeval tv;
54 gettimeofday(&tv, NULL);
55 return tv.tv_sec + tv.tv_usec / 1000000.0;
56}
57
58ScreenRecoveryUI::ScreenRecoveryUI() :
59 currentIcon(NONE),
60 installingFrame(0),
61 progressBarType(EMPTY),
62 progressScopeStart(0),
63 progressScopeSize(0),
64 progress(0),
65 pagesIdentical(false),
66 text_cols(0),
67 text_rows(0),
68 text_col(0),
69 text_row(0),
70 text_top(0),
71 show_text(false),
72 show_text_ever(false),
73 show_menu(false),
74 menu_top(0),
75 menu_items(0),
76 menu_sel(0),
Doug Zongker32a0a472011-11-01 11:00:20 -070077
78 // These values are correct for the default image resources
79 // provided with the android platform. Devices which use
80 // different resources should have a subclass of ScreenRecoveryUI
81 // that overrides Init() to set these values appropriately and
82 // then call the superclass Init().
83 animation_fps(20),
84 indeterminate_frames(6),
85 installing_frames(7),
86 install_overlay_offset_x(13),
87 install_overlay_offset_y(190) {
Doug Zongker211aebc2011-10-28 15:13:10 -070088 pthread_mutex_init(&updateMutex, NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -070089 self = this;
90}
91
92// Draw the given frame over the installation overlay animation. The
93// background is not cleared or draw with the base icon first; we
94// assume that the frame already contains some other frame of the
95// animation. Does nothing if no overlay animation is defined.
96// Should only be called with updateMutex locked.
97void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
98 if (installationOverlay == NULL) return;
99 gr_surface surface = installationOverlay[frame];
100 int iconWidth = gr_get_width(surface);
101 int iconHeight = gr_get_height(surface);
102 gr_blit(surface, 0, 0, iconWidth, iconHeight,
Doug Zongker32a0a472011-11-01 11:00:20 -0700103 install_overlay_offset_x, install_overlay_offset_y);
Doug Zongker211aebc2011-10-28 15:13:10 -0700104}
105
106// Clear the screen and draw the currently selected background icon (if any).
107// Should only be called with updateMutex locked.
108void ScreenRecoveryUI::draw_background_locked(Icon icon)
109{
110 pagesIdentical = false;
111 gr_color(0, 0, 0, 255);
112 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
113
114 if (icon) {
115 gr_surface surface = backgroundIcon[icon];
116 int iconWidth = gr_get_width(surface);
117 int iconHeight = gr_get_height(surface);
118 int iconX = (gr_fb_width() - iconWidth) / 2;
119 int iconY = (gr_fb_height() - iconHeight) / 2;
120 gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
121 if (icon == INSTALLING) {
122 draw_install_overlay_locked(installingFrame);
123 }
124 }
125}
126
127// Draw the progress bar (if any) on the screen. Does not flip pages.
128// Should only be called with updateMutex locked.
129void ScreenRecoveryUI::draw_progress_locked()
130{
Doug Zongker69f4b672012-04-26 14:37:53 -0700131 if (currentIcon == ERROR) return;
132
Doug Zongker211aebc2011-10-28 15:13:10 -0700133 if (currentIcon == INSTALLING) {
134 draw_install_overlay_locked(installingFrame);
135 }
136
137 if (progressBarType != EMPTY) {
138 int iconHeight = gr_get_height(backgroundIcon[INSTALLING]);
139 int width = gr_get_width(progressBarEmpty);
140 int height = gr_get_height(progressBarEmpty);
141
142 int dx = (gr_fb_width() - width)/2;
143 int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
144
145 // Erase behind the progress bar (in case this was a progress-only update)
146 gr_color(0, 0, 0, 255);
147 gr_fill(dx, dy, width, height);
148
149 if (progressBarType == DETERMINATE) {
150 float p = progressScopeStart + progress * progressScopeSize;
151 int pos = (int) (p * width);
152
153 if (pos > 0) {
154 gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
155 }
156 if (pos < width-1) {
157 gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
158 }
159 }
160
161 if (progressBarType == INDETERMINATE) {
162 static int frame = 0;
163 gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
Doug Zongker32a0a472011-11-01 11:00:20 -0700164 frame = (frame + 1) % indeterminate_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700165 }
166 }
167}
168
169void ScreenRecoveryUI::draw_text_line(int row, const char* t) {
170 if (t[0] != '\0') {
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 twgr_text(0, (row+1)*CHAR_HEIGHT-1, t);
Doug Zongker211aebc2011-10-28 15:13:10 -0700172 }
173}
174
175// Redraw everything on the screen. Does not flip pages.
176// Should only be called with updateMutex locked.
177void ScreenRecoveryUI::draw_screen_locked()
178{
179 draw_background_locked(currentIcon);
180 draw_progress_locked();
181
182 if (show_text) {
183 gr_color(0, 0, 0, 160);
184 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
185
186 int i = 0;
187 if (show_menu) {
188 gr_color(64, 96, 255, 255);
189 gr_fill(0, (menu_top+menu_sel) * CHAR_HEIGHT,
190 gr_fb_width(), (menu_top+menu_sel+1)*CHAR_HEIGHT+1);
191
192 for (; i < menu_top + menu_items; ++i) {
193 if (i == menu_top + menu_sel) {
194 gr_color(255, 255, 255, 255);
195 draw_text_line(i, menu[i]);
196 gr_color(64, 96, 255, 255);
197 } else {
198 draw_text_line(i, menu[i]);
199 }
200 }
201 gr_fill(0, i*CHAR_HEIGHT+CHAR_HEIGHT/2-1,
202 gr_fb_width(), i*CHAR_HEIGHT+CHAR_HEIGHT/2+1);
203 ++i;
204 }
205
206 gr_color(255, 255, 0, 255);
207
208 for (; i < text_rows; ++i) {
209 draw_text_line(i, text[(i+text_top) % text_rows]);
210 }
211 }
212}
213
214// Redraw everything on the screen and flip the screen (make it visible).
215// Should only be called with updateMutex locked.
216void ScreenRecoveryUI::update_screen_locked()
217{
218 draw_screen_locked();
219 gr_flip();
220}
221
222// Updates only the progress bar, if possible, otherwise redraws the screen.
223// Should only be called with updateMutex locked.
224void ScreenRecoveryUI::update_progress_locked()
Dees_Troy32c8eb82012-09-11 15:28:06 -0400225{return;
Doug Zongker211aebc2011-10-28 15:13:10 -0700226 if (show_text || !pagesIdentical) {
227 draw_screen_locked(); // Must redraw the whole screen
228 pagesIdentical = true;
229 } else {
230 draw_progress_locked(); // Draw only the progress bar and overlays
231 }
232 gr_flip();
233}
234
235// Keeps the progress bar updated, even when the process is otherwise busy.
Doug Zongker32a0a472011-11-01 11:00:20 -0700236void* ScreenRecoveryUI::progress_thread(void *cookie) {
237 self->progress_loop();
238 return NULL;
239}
240
241void ScreenRecoveryUI::progress_loop() {
242 double interval = 1.0 / animation_fps;
Doug Zongker211aebc2011-10-28 15:13:10 -0700243 for (;;) {
244 double start = now();
Doug Zongker32a0a472011-11-01 11:00:20 -0700245 pthread_mutex_lock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700246
247 int redraw = 0;
248
249 // update the installation animation, if active
250 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700251 if (currentIcon == INSTALLING && installing_frames > 0 && !show_text) {
252 installingFrame = (installingFrame + 1) % installing_frames;
Doug Zongker211aebc2011-10-28 15:13:10 -0700253 redraw = 1;
254 }
255
256 // update the progress bar animation, if active
257 // skip this if we have a text overlay (too expensive to update)
Doug Zongker32a0a472011-11-01 11:00:20 -0700258 if (progressBarType == INDETERMINATE && !show_text) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700259 redraw = 1;
260 }
261
262 // move the progress bar forward on timed intervals, if configured
Doug Zongker32a0a472011-11-01 11:00:20 -0700263 int duration = progressScopeDuration;
264 if (progressBarType == DETERMINATE && duration > 0) {
265 double elapsed = now() - progressScopeTime;
Doug Zongker69f4b672012-04-26 14:37:53 -0700266 float p = 1.0 * elapsed / duration;
267 if (p > 1.0) p = 1.0;
268 if (p > progress) {
269 progress = p;
Doug Zongker211aebc2011-10-28 15:13:10 -0700270 redraw = 1;
271 }
272 }
273
Doug Zongker32a0a472011-11-01 11:00:20 -0700274 if (redraw) update_progress_locked();
Doug Zongker211aebc2011-10-28 15:13:10 -0700275
Doug Zongker32a0a472011-11-01 11:00:20 -0700276 pthread_mutex_unlock(&updateMutex);
Doug Zongker211aebc2011-10-28 15:13:10 -0700277 double end = now();
278 // minimum of 20ms delay between frames
279 double delay = interval - (end-start);
280 if (delay < 0.02) delay = 0.02;
281 usleep((long)(delay * 1000000));
282 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700283}
284
285void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
286 int result = res_create_surface(filename, surface);
287 if (result < 0) {
288 LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
289 }
290}
291
292void ScreenRecoveryUI::Init()
293{
294 gr_init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700295
296 text_col = text_row = 0;
297 text_rows = gr_fb_height() / CHAR_HEIGHT;
298 if (text_rows > kMaxRows) text_rows = kMaxRows;
299 text_top = 1;
300
301 text_cols = gr_fb_width() / CHAR_WIDTH;
302 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
303
304 LoadBitmap("icon_installing", &backgroundIcon[INSTALLING]);
305 LoadBitmap("icon_error", &backgroundIcon[ERROR]);
306 LoadBitmap("progress_empty", &progressBarEmpty);
307 LoadBitmap("progress_fill", &progressBarFill);
308
309 int i;
310
Doug Zongker32a0a472011-11-01 11:00:20 -0700311 progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700312 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700313 for (i = 0; i < indeterminate_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 char filename[40];
315 // "indeterminate01.png", "indeterminate02.png", ...
316 sprintf(filename, "indeterminate%02d", i+1);
317 LoadBitmap(filename, progressBarIndeterminate+i);
318 }
319
Doug Zongker32a0a472011-11-01 11:00:20 -0700320 if (installing_frames > 0) {
321 installationOverlay = (gr_surface*)malloc(installing_frames *
Doug Zongker211aebc2011-10-28 15:13:10 -0700322 sizeof(gr_surface));
Doug Zongker32a0a472011-11-01 11:00:20 -0700323 for (i = 0; i < installing_frames; ++i) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700324 char filename[40];
325 // "icon_installing_overlay01.png",
326 // "icon_installing_overlay02.png", ...
327 sprintf(filename, "icon_installing_overlay%02d", i+1);
328 LoadBitmap(filename, installationOverlay+i);
329 }
330
331 // Adjust the offset to account for the positioning of the
332 // base image on the screen.
333 if (backgroundIcon[INSTALLING] != NULL) {
334 gr_surface bg = backgroundIcon[INSTALLING];
Doug Zongker32a0a472011-11-01 11:00:20 -0700335 install_overlay_offset_x += (gr_fb_width() - gr_get_width(bg)) / 2;
336 install_overlay_offset_y += (gr_fb_height() - gr_get_height(bg)) / 2;
Doug Zongker211aebc2011-10-28 15:13:10 -0700337 }
338 } else {
339 installationOverlay = NULL;
340 }
341
342 pthread_create(&progress_t, NULL, progress_thread, NULL);
Doug Zongker32a0a472011-11-01 11:00:20 -0700343
344 RecoveryUI::Init();
Doug Zongker211aebc2011-10-28 15:13:10 -0700345}
346
347void ScreenRecoveryUI::SetBackground(Icon icon)
348{
349 pthread_mutex_lock(&updateMutex);
350 currentIcon = icon;
351 update_screen_locked();
352 pthread_mutex_unlock(&updateMutex);
353}
354
355void ScreenRecoveryUI::SetProgressType(ProgressType type)
356{
357 pthread_mutex_lock(&updateMutex);
358 if (progressBarType != type) {
359 progressBarType = type;
360 update_progress_locked();
361 }
Doug Zongker69f4b672012-04-26 14:37:53 -0700362 progressScopeStart = 0;
363 progress = 0;
Doug Zongker211aebc2011-10-28 15:13:10 -0700364 pthread_mutex_unlock(&updateMutex);
365}
366
367void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
368{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400369 DataManager::SetValue("ui_progress_portion", (float)(portion * 100.0));
370 DataManager::SetValue("ui_progress_frames", seconds * 30);
371
Doug Zongker211aebc2011-10-28 15:13:10 -0700372 pthread_mutex_lock(&updateMutex);
373 progressBarType = DETERMINATE;
374 progressScopeStart += progressScopeSize;
375 progressScopeSize = portion;
376 progressScopeTime = now();
377 progressScopeDuration = seconds;
378 progress = 0;
379 update_progress_locked();
380 pthread_mutex_unlock(&updateMutex);
381}
382
383void ScreenRecoveryUI::SetProgress(float fraction)
384{
Dees_Troy32c8eb82012-09-11 15:28:06 -0400385 DataManager::SetValue("ui_progress", (float) (fraction * 100.0)); return;
386
Doug Zongker211aebc2011-10-28 15:13:10 -0700387 pthread_mutex_lock(&updateMutex);
388 if (fraction < 0.0) fraction = 0.0;
389 if (fraction > 1.0) fraction = 1.0;
390 if (progressBarType == DETERMINATE && fraction > progress) {
391 // Skip updates that aren't visibly different.
392 int width = gr_get_width(progressBarIndeterminate[0]);
393 float scale = width * progressScopeSize;
394 if ((int) (progress * scale) != (int) (fraction * scale)) {
395 progress = fraction;
396 update_progress_locked();
397 }
398 }
399 pthread_mutex_unlock(&updateMutex);
400}
401
402void ScreenRecoveryUI::Print(const char *fmt, ...)
403{
404 char buf[256];
405 va_list ap;
406 va_start(ap, fmt);
407 vsnprintf(buf, 256, fmt, ap);
408 va_end(ap);
409
Dees_Troy32c8eb82012-09-11 15:28:06 -0400410 gui_print("%s", buf);
411 return;
412
Doug Zongker211aebc2011-10-28 15:13:10 -0700413 fputs(buf, stdout);
414
415 // This can get called before ui_init(), so be careful.
416 pthread_mutex_lock(&updateMutex);
417 if (text_rows > 0 && text_cols > 0) {
418 char *ptr;
419 for (ptr = buf; *ptr != '\0'; ++ptr) {
420 if (*ptr == '\n' || text_col >= text_cols) {
421 text[text_row][text_col] = '\0';
422 text_col = 0;
423 text_row = (text_row + 1) % text_rows;
424 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
425 }
426 if (*ptr != '\n') text[text_row][text_col++] = *ptr;
427 }
428 text[text_row][text_col] = '\0';
429 update_screen_locked();
430 }
431 pthread_mutex_unlock(&updateMutex);
432}
433
434void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
435 int initial_selection) {
436 int i;
437 pthread_mutex_lock(&updateMutex);
438 if (text_rows > 0 && text_cols > 0) {
439 for (i = 0; i < text_rows; ++i) {
440 if (headers[i] == NULL) break;
441 strncpy(menu[i], headers[i], text_cols-1);
442 menu[i][text_cols-1] = '\0';
443 }
444 menu_top = i;
445 for (; i < text_rows; ++i) {
446 if (items[i-menu_top] == NULL) break;
447 strncpy(menu[i], items[i-menu_top], text_cols-1);
448 menu[i][text_cols-1] = '\0';
449 }
450 menu_items = i - menu_top;
451 show_menu = 1;
452 menu_sel = initial_selection;
453 update_screen_locked();
454 }
455 pthread_mutex_unlock(&updateMutex);
456}
457
458int ScreenRecoveryUI::SelectMenu(int sel) {
459 int old_sel;
460 pthread_mutex_lock(&updateMutex);
461 if (show_menu > 0) {
462 old_sel = menu_sel;
463 menu_sel = sel;
464 if (menu_sel < 0) menu_sel = 0;
465 if (menu_sel >= menu_items) menu_sel = menu_items-1;
466 sel = menu_sel;
467 if (menu_sel != old_sel) update_screen_locked();
468 }
469 pthread_mutex_unlock(&updateMutex);
470 return sel;
471}
472
473void ScreenRecoveryUI::EndMenu() {
474 int i;
475 pthread_mutex_lock(&updateMutex);
476 if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
477 show_menu = 0;
478 update_screen_locked();
479 }
480 pthread_mutex_unlock(&updateMutex);
481}
482
483bool ScreenRecoveryUI::IsTextVisible()
484{
485 pthread_mutex_lock(&updateMutex);
486 int visible = show_text;
487 pthread_mutex_unlock(&updateMutex);
488 return visible;
489}
490
491bool ScreenRecoveryUI::WasTextEverVisible()
492{
493 pthread_mutex_lock(&updateMutex);
494 int ever_visible = show_text_ever;
495 pthread_mutex_unlock(&updateMutex);
496 return ever_visible;
497}
498
499void ScreenRecoveryUI::ShowText(bool visible)
500{
501 pthread_mutex_lock(&updateMutex);
502 show_text = visible;
503 if (show_text) show_text_ever = 1;
504 update_screen_locked();
505 pthread_mutex_unlock(&updateMutex);
506}