• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
32 #include "device.h"
33 #include "minui/minui.h"
34 #include "screen_ui.h"
35 #include "ui.h"
36 
37 static int char_width;
38 static int char_height;
39 
40 // 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.
43 static ScreenRecoveryUI* self = NULL;
44 
45 // Return the current time as a double (including fractions of a second).
now()46 static double now() {
47     struct timeval tv;
48     gettimeofday(&tv, NULL);
49     return tv.tv_sec + tv.tv_usec / 1000000.0;
50 }
51 
ScreenRecoveryUI()52 ScreenRecoveryUI::ScreenRecoveryUI() :
53     currentIcon(NONE),
54     installingFrame(0),
55     rtl_locale(false),
56     progressBarType(EMPTY),
57     progressScopeStart(0),
58     progressScopeSize(0),
59     progress(0),
60     pagesIdentical(false),
61     text_cols(0),
62     text_rows(0),
63     text_col(0),
64     text_row(0),
65     text_top(0),
66     show_text(false),
67     show_text_ever(false),
68     show_menu(false),
69     menu_top(0),
70     menu_items(0),
71     menu_sel(0),
72 
73     // These values are correct for the default image resources
74     // provided with the android platform.  Devices which use
75     // different resources should have a subclass of ScreenRecoveryUI
76     // that overrides Init() to set these values appropriately and
77     // then call the superclass Init().
78     animation_fps(20),
79     indeterminate_frames(6),
80     installing_frames(7),
81     install_overlay_offset_x(13),
82     install_overlay_offset_y(190),
83     overlay_offset_x(-1),
84     overlay_offset_y(-1) {
85     pthread_mutex_init(&updateMutex, NULL);
86     self = this;
87 }
88 
89 // Draw the given frame over the installation overlay animation.  The
90 // background is not cleared or draw with the base icon first; we
91 // assume that the frame already contains some other frame of the
92 // animation.  Does nothing if no overlay animation is defined.
93 // Should only be called with updateMutex locked.
draw_install_overlay_locked(int frame)94 void ScreenRecoveryUI::draw_install_overlay_locked(int frame) {
95     if (installationOverlay == NULL || overlay_offset_x < 0) return;
96     gr_surface surface = installationOverlay[frame];
97     int iconWidth = gr_get_width(surface);
98     int iconHeight = gr_get_height(surface);
99     gr_blit(surface, 0, 0, iconWidth, iconHeight,
100             overlay_offset_x, overlay_offset_y);
101 }
102 
103 // Clear the screen and draw the currently selected background icon (if any).
104 // Should only be called with updateMutex locked.
draw_background_locked(Icon icon)105 void ScreenRecoveryUI::draw_background_locked(Icon icon)
106 {
107     pagesIdentical = false;
108     gr_color(0, 0, 0, 255);
109     gr_fill(0, 0, gr_fb_width(), gr_fb_height());
110 
111     if (icon) {
112         gr_surface surface = backgroundIcon[icon];
113         gr_surface text_surface = backgroundText[icon];
114 
115         int iconWidth = gr_get_width(surface);
116         int iconHeight = gr_get_height(surface);
117         int textWidth = gr_get_width(text_surface);
118         int textHeight = gr_get_height(text_surface);
119 
120         int iconX = (gr_fb_width() - iconWidth) / 2;
121         int iconY = (gr_fb_height() - (iconHeight+textHeight+40)) / 2;
122 
123         int textX = (gr_fb_width() - textWidth) / 2;
124         int textY = ((gr_fb_height() - (iconHeight+textHeight+40)) / 2) + iconHeight + 40;
125 
126         gr_blit(surface, 0, 0, iconWidth, iconHeight, iconX, iconY);
127         if (icon == INSTALLING_UPDATE || icon == ERASING) {
128             draw_install_overlay_locked(installingFrame);
129         }
130 
131         gr_color(255, 255, 255, 255);
132         gr_texticon(textX, textY, text_surface);
133     }
134 }
135 
136 // Draw the progress bar (if any) on the screen.  Does not flip pages.
137 // Should only be called with updateMutex locked.
draw_progress_locked()138 void ScreenRecoveryUI::draw_progress_locked()
139 {
140     if (currentIcon == ERROR) return;
141 
142     if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
143         draw_install_overlay_locked(installingFrame);
144     }
145 
146     if (progressBarType != EMPTY) {
147         int iconHeight = gr_get_height(backgroundIcon[INSTALLING_UPDATE]);
148         int width = gr_get_width(progressBarEmpty);
149         int height = gr_get_height(progressBarEmpty);
150 
151         int dx = (gr_fb_width() - width)/2;
152         int dy = (3*gr_fb_height() + iconHeight - 2*height)/4;
153 
154         // Erase behind the progress bar (in case this was a progress-only update)
155         gr_color(0, 0, 0, 255);
156         gr_fill(dx, dy, width, height);
157 
158         if (progressBarType == DETERMINATE) {
159             float p = progressScopeStart + progress * progressScopeSize;
160             int pos = (int) (p * width);
161 
162             if (rtl_locale) {
163                 // Fill the progress bar from right to left.
164                 if (pos > 0) {
165                     gr_blit(progressBarFill, width-pos, 0, pos, height, dx+width-pos, dy);
166                 }
167                 if (pos < width-1) {
168                     gr_blit(progressBarEmpty, 0, 0, width-pos, height, dx, dy);
169                 }
170             } else {
171                 // Fill the progress bar from left to right.
172                 if (pos > 0) {
173                     gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
174                 }
175                 if (pos < width-1) {
176                     gr_blit(progressBarEmpty, pos, 0, width-pos, height, dx+pos, dy);
177                 }
178             }
179         }
180 
181         if (progressBarType == INDETERMINATE) {
182             static int frame = 0;
183             gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy);
184             // in RTL locales, we run the animation backwards, which
185             // makes the spinner spin the other way.
186             if (rtl_locale) {
187                 frame = (frame + indeterminate_frames - 1) % indeterminate_frames;
188             } else {
189                 frame = (frame + 1) % indeterminate_frames;
190             }
191         }
192     }
193 }
194 
195 #define C_HEADER  247,0,6
196 #define C_MENU    0,106,157
197 #define C_LOG     249,194,0
198 
199 // Redraw everything on the screen.  Does not flip pages.
200 // Should only be called with updateMutex locked.
draw_screen_locked()201 void ScreenRecoveryUI::draw_screen_locked()
202 {
203     draw_background_locked(currentIcon);
204     draw_progress_locked();
205 
206     if (show_text) {
207         gr_color(0, 0, 0, 160);
208         gr_fill(0, 0, gr_fb_width(), gr_fb_height());
209 
210         int y = 0;
211         int i = 0;
212         if (show_menu) {
213             gr_color(C_HEADER, 255);
214 
215             for (; i < menu_top + menu_items; ++i) {
216                 if (i == menu_top) gr_color(C_MENU, 255);
217 
218                 if (i == menu_top + menu_sel) {
219                     // draw the highlight bar
220                     gr_fill(0, y-2, gr_fb_width(), y+char_height+2);
221                     // white text of selected item
222                     gr_color(255, 255, 255, 255);
223                     if (menu[i][0]) gr_text(4, y, menu[i], 1);
224                     gr_color(C_MENU, 255);
225                 } else {
226                     if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top);
227                 }
228                 y += char_height+4;
229             }
230             gr_color(C_MENU, 255);
231             y += 4;
232             gr_fill(0, y, gr_fb_width(), y+2);
233             y += 4;
234             ++i;
235         }
236 
237         gr_color(C_LOG, 255);
238 
239         // display from the bottom up, until we hit the top of the
240         // screen, the bottom of the menu, or we've displayed the
241         // entire text buffer.
242         int ty;
243         int row = (text_top+text_rows-1) % text_rows;
244         for (int ty = gr_fb_height() - char_height, count = 0;
245              ty > y+2 && count < text_rows;
246              ty -= char_height, ++count) {
247             gr_text(4, ty, text[row], 0);
248             --row;
249             if (row < 0) row = text_rows-1;
250         }
251     }
252 }
253 
254 // Redraw everything on the screen and flip the screen (make it visible).
255 // Should only be called with updateMutex locked.
update_screen_locked()256 void ScreenRecoveryUI::update_screen_locked()
257 {
258     draw_screen_locked();
259     gr_flip();
260 }
261 
262 // Updates only the progress bar, if possible, otherwise redraws the screen.
263 // Should only be called with updateMutex locked.
update_progress_locked()264 void ScreenRecoveryUI::update_progress_locked()
265 {
266     if (show_text || !pagesIdentical) {
267         draw_screen_locked();    // Must redraw the whole screen
268         pagesIdentical = true;
269     } else {
270         draw_progress_locked();  // Draw only the progress bar and overlays
271     }
272     gr_flip();
273 }
274 
275 // Keeps the progress bar updated, even when the process is otherwise busy.
progress_thread(void * cookie)276 void* ScreenRecoveryUI::progress_thread(void *cookie) {
277     self->progress_loop();
278     return NULL;
279 }
280 
progress_loop()281 void ScreenRecoveryUI::progress_loop() {
282     double interval = 1.0 / animation_fps;
283     for (;;) {
284         double start = now();
285         pthread_mutex_lock(&updateMutex);
286 
287         int redraw = 0;
288 
289         // update the installation animation, if active
290         // skip this if we have a text overlay (too expensive to update)
291         if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) &&
292             installing_frames > 0 && !show_text) {
293             installingFrame = (installingFrame + 1) % installing_frames;
294             redraw = 1;
295         }
296 
297         // update the progress bar animation, if active
298         // skip this if we have a text overlay (too expensive to update)
299         if (progressBarType == INDETERMINATE && !show_text) {
300             redraw = 1;
301         }
302 
303         // move the progress bar forward on timed intervals, if configured
304         int duration = progressScopeDuration;
305         if (progressBarType == DETERMINATE && duration > 0) {
306             double elapsed = now() - progressScopeTime;
307             float p = 1.0 * elapsed / duration;
308             if (p > 1.0) p = 1.0;
309             if (p > progress) {
310                 progress = p;
311                 redraw = 1;
312             }
313         }
314 
315         if (redraw) update_progress_locked();
316 
317         pthread_mutex_unlock(&updateMutex);
318         double end = now();
319         // minimum of 20ms delay between frames
320         double delay = interval - (end-start);
321         if (delay < 0.02) delay = 0.02;
322         usleep((long)(delay * 1000000));
323     }
324 }
325 
LoadBitmap(const char * filename,gr_surface * surface)326 void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) {
327     int result = res_create_surface(filename, surface);
328     if (result < 0) {
329         LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
330     }
331 }
332 
LoadLocalizedBitmap(const char * filename,gr_surface * surface)333 void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) {
334     int result = res_create_localized_surface(filename, surface);
335     if (result < 0) {
336         LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
337     }
338 }
339 
Init()340 void ScreenRecoveryUI::Init()
341 {
342     gr_init();
343 
344     gr_font_size(&char_width, &char_height);
345 
346     text_col = text_row = 0;
347     text_rows = gr_fb_height() / char_height;
348     if (text_rows > kMaxRows) text_rows = kMaxRows;
349     text_top = 1;
350 
351     text_cols = gr_fb_width() / char_width;
352     if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
353 
354     LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
355     backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
356     LoadBitmap("icon_error", &backgroundIcon[ERROR]);
357     backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
358 
359     LoadBitmap("progress_empty", &progressBarEmpty);
360     LoadBitmap("progress_fill", &progressBarFill);
361 
362     LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
363     LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
364     LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
365     LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
366 
367     int i;
368 
369     progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames *
370                                                     sizeof(gr_surface));
371     for (i = 0; i < indeterminate_frames; ++i) {
372         char filename[40];
373         // "indeterminate01.png", "indeterminate02.png", ...
374         sprintf(filename, "indeterminate%02d", i+1);
375         LoadBitmap(filename, progressBarIndeterminate+i);
376     }
377 
378     if (installing_frames > 0) {
379         installationOverlay = (gr_surface*)malloc(installing_frames *
380                                                    sizeof(gr_surface));
381         for (i = 0; i < installing_frames; ++i) {
382             char filename[40];
383             // "icon_installing_overlay01.png",
384             // "icon_installing_overlay02.png", ...
385             sprintf(filename, "icon_installing_overlay%02d", i+1);
386             LoadBitmap(filename, installationOverlay+i);
387         }
388     } else {
389         installationOverlay = NULL;
390     }
391 
392     pthread_create(&progress_t, NULL, progress_thread, NULL);
393 
394     RecoveryUI::Init();
395 }
396 
SetLocale(const char * locale)397 void ScreenRecoveryUI::SetLocale(const char* locale) {
398     if (locale) {
399         char* lang = strdup(locale);
400         for (char* p = lang; *p; ++p) {
401             if (*p == '_') {
402                 *p = '\0';
403                 break;
404             }
405         }
406 
407         // A bit cheesy: keep an explicit list of supported languages
408         // that are RTL.
409         if (strcmp(lang, "ar") == 0 ||   // Arabic
410             strcmp(lang, "fa") == 0 ||   // Persian (Farsi)
411             strcmp(lang, "he") == 0 ||   // Hebrew (new language code)
412             strcmp(lang, "iw") == 0 ||   // Hebrew (old language code)
413             strcmp(lang, "ur") == 0) {   // Urdu
414             rtl_locale = true;
415         }
416         free(lang);
417     }
418 }
419 
SetBackground(Icon icon)420 void ScreenRecoveryUI::SetBackground(Icon icon)
421 {
422     pthread_mutex_lock(&updateMutex);
423 
424     // Adjust the offset to account for the positioning of the
425     // base image on the screen.
426     if (backgroundIcon[icon] != NULL) {
427         gr_surface bg = backgroundIcon[icon];
428         gr_surface text = backgroundText[icon];
429         overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2;
430         overlay_offset_y = install_overlay_offset_y +
431             (gr_fb_height() - (gr_get_height(bg) + gr_get_height(text) + 40)) / 2;
432     }
433 
434     currentIcon = icon;
435     update_screen_locked();
436 
437     pthread_mutex_unlock(&updateMutex);
438 }
439 
SetProgressType(ProgressType type)440 void ScreenRecoveryUI::SetProgressType(ProgressType type)
441 {
442     pthread_mutex_lock(&updateMutex);
443     if (progressBarType != type) {
444         progressBarType = type;
445         update_progress_locked();
446     }
447     progressScopeStart = 0;
448     progress = 0;
449     pthread_mutex_unlock(&updateMutex);
450 }
451 
ShowProgress(float portion,float seconds)452 void ScreenRecoveryUI::ShowProgress(float portion, float seconds)
453 {
454     pthread_mutex_lock(&updateMutex);
455     progressBarType = DETERMINATE;
456     progressScopeStart += progressScopeSize;
457     progressScopeSize = portion;
458     progressScopeTime = now();
459     progressScopeDuration = seconds;
460     progress = 0;
461     update_progress_locked();
462     pthread_mutex_unlock(&updateMutex);
463 }
464 
SetProgress(float fraction)465 void ScreenRecoveryUI::SetProgress(float fraction)
466 {
467     pthread_mutex_lock(&updateMutex);
468     if (fraction < 0.0) fraction = 0.0;
469     if (fraction > 1.0) fraction = 1.0;
470     if (progressBarType == DETERMINATE && fraction > progress) {
471         // Skip updates that aren't visibly different.
472         int width = gr_get_width(progressBarIndeterminate[0]);
473         float scale = width * progressScopeSize;
474         if ((int) (progress * scale) != (int) (fraction * scale)) {
475             progress = fraction;
476             update_progress_locked();
477         }
478     }
479     pthread_mutex_unlock(&updateMutex);
480 }
481 
Print(const char * fmt,...)482 void ScreenRecoveryUI::Print(const char *fmt, ...)
483 {
484     char buf[256];
485     va_list ap;
486     va_start(ap, fmt);
487     vsnprintf(buf, 256, fmt, ap);
488     va_end(ap);
489 
490     fputs(buf, stdout);
491 
492     // This can get called before ui_init(), so be careful.
493     pthread_mutex_lock(&updateMutex);
494     if (text_rows > 0 && text_cols > 0) {
495         char *ptr;
496         for (ptr = buf; *ptr != '\0'; ++ptr) {
497             if (*ptr == '\n' || text_col >= text_cols) {
498                 text[text_row][text_col] = '\0';
499                 text_col = 0;
500                 text_row = (text_row + 1) % text_rows;
501                 if (text_row == text_top) text_top = (text_top + 1) % text_rows;
502             }
503             if (*ptr != '\n') text[text_row][text_col++] = *ptr;
504         }
505         text[text_row][text_col] = '\0';
506         update_screen_locked();
507     }
508     pthread_mutex_unlock(&updateMutex);
509 }
510 
StartMenu(const char * const * headers,const char * const * items,int initial_selection)511 void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
512                                  int initial_selection) {
513     int i;
514     pthread_mutex_lock(&updateMutex);
515     if (text_rows > 0 && text_cols > 0) {
516         for (i = 0; i < text_rows; ++i) {
517             if (headers[i] == NULL) break;
518             strncpy(menu[i], headers[i], text_cols-1);
519             menu[i][text_cols-1] = '\0';
520         }
521         menu_top = i;
522         for (; i < text_rows; ++i) {
523             if (items[i-menu_top] == NULL) break;
524             strncpy(menu[i], items[i-menu_top], text_cols-1);
525             menu[i][text_cols-1] = '\0';
526         }
527         menu_items = i - menu_top;
528         show_menu = 1;
529         menu_sel = initial_selection;
530         update_screen_locked();
531     }
532     pthread_mutex_unlock(&updateMutex);
533 }
534 
SelectMenu(int sel)535 int ScreenRecoveryUI::SelectMenu(int sel) {
536     int old_sel;
537     pthread_mutex_lock(&updateMutex);
538     if (show_menu > 0) {
539         old_sel = menu_sel;
540         menu_sel = sel;
541         if (menu_sel < 0) menu_sel = 0;
542         if (menu_sel >= menu_items) menu_sel = menu_items-1;
543         sel = menu_sel;
544         if (menu_sel != old_sel) update_screen_locked();
545     }
546     pthread_mutex_unlock(&updateMutex);
547     return sel;
548 }
549 
EndMenu()550 void ScreenRecoveryUI::EndMenu() {
551     int i;
552     pthread_mutex_lock(&updateMutex);
553     if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
554         show_menu = 0;
555         update_screen_locked();
556     }
557     pthread_mutex_unlock(&updateMutex);
558 }
559 
IsTextVisible()560 bool ScreenRecoveryUI::IsTextVisible()
561 {
562     pthread_mutex_lock(&updateMutex);
563     int visible = show_text;
564     pthread_mutex_unlock(&updateMutex);
565     return visible;
566 }
567 
WasTextEverVisible()568 bool ScreenRecoveryUI::WasTextEverVisible()
569 {
570     pthread_mutex_lock(&updateMutex);
571     int ever_visible = show_text_ever;
572     pthread_mutex_unlock(&updateMutex);
573     return ever_visible;
574 }
575 
ShowText(bool visible)576 void ScreenRecoveryUI::ShowText(bool visible)
577 {
578     pthread_mutex_lock(&updateMutex);
579     show_text = visible;
580     if (show_text) show_text_ever = 1;
581     update_screen_locked();
582     pthread_mutex_unlock(&updateMutex);
583 }
584