What is Android recovery mode?
There are countless answers to this question on Baidu. A friend who doesn't understand is a tutor first.
From a purely technical point of view, recovery and Android are essentially two separate rootfs, just recovery this rootfs existence is meant for Android, a Rootfs service, and is therefore interpreted as part of the Android system.
recovery as a simple rootfs, provides a very limited number of features, including only a few simple libraries, UI display is used in the form of direct brush framebuffer, as the Android framework and app layer of the Code farm, In this form is relatively unfamiliar, special time to comb a bit. first, look at the UI-related statements in the main function code of Reocvery
Main (int argc, char **argv) { ... device* device = Make_device (); UI = Device->getui (); Gcurrentui = UI; Ui->init (); Ui->setlocale (locale); Ui->setbackground (recoveryui::none); if (Show_text) Ui->showtext (true); ...... if (Status! = Install_success | | ui->istextvisible ()) { prompt_and_wait (device, status); } ......}
1, the first new object of a device class, the device class encapsulates a number of operations, including the operation of the UI2, call the device Class Getui () to return a Defaultui object, recovery involves three UI classes, three classes are inheritance relationships, respectively, Defaultui, Screenrecoveryui, Recoveryui3. Call Init () of the Defaultui class, the Defaultui class does not have the init () method, so it will call its parent class Screenrecoveryui init ()4. Similarly. Call the setlocale () of the Screenrecoveryui class to identify a few specific areas5. Similarly. Call the Screenrecoveryui class's SetBackground () to set the initial state of the background map6. Display the main interface of recovery, that is, a selection menu
void Screenrecoveryui::init () {gr_init (); Gr_font_size (&char_width, &char_height); Text_col = Text_row = 0; Text_rows = Gr_fb_height ()/char_height; if (Text_rows > kmaxrows) text_rows = kmaxrows; Text_top = 1; Text_cols = Gr_fb_width ()/char_width; if (Text_cols > kMaxCols-1) text_cols = kMaxCols-1; LoadBitmap ("icon_installing", &backgroundicon[installing_update]); Backgroundicon[erasing] = backgroundicon[installing_update]; LoadBitmap ("Icon_error", &backgroundicon[error]); Backgroundicon[no_command] = Backgroundicon[error]; LoadBitmap ("Progress_empty", &progressbarempty); LoadBitmap ("Progress_fill", &progressbarfill); Loadlocalizedbitmap ("Installing_text", &backgroundtext[installing_update]); Loadlocalizedbitmap ("Erasing_text", &backgroundtext[erasing]); Loadlocalizedbitmap ("No_command_text", &backgroundtext[no_command]); Loadlocalizedbitmap ("Error_text", &backgroundtext[error]); int i; Progressbarindeterminate = (gr_surface*) malloc (Indeterminate_frames * s Izeof (Gr_surface)); for (i = 0; i < indeterminate_frames; ++i) {char filename[40]; "Indeterminate01.png", "indeterminate02.png", ... sprintf (filename, "indeterminate%02d", i+1); LoadBitmap (filename, progressbarindeterminate+i); } if (Installing_frames > 0) {installationoverlay = (gr_surface*) malloc (Installing_frames * sizeof (Gr_surface)); for (i = 0; i < installing_frames; ++i) {char filename[40]; "Icon_installing_overlay01.png",//"Icon_installing_overlay02.png", ... sprintf (filename, "Icon_ installing_overlay%02d ", i+1); LoadBitmap (filename, installationoverlay+i); }} else {installationoverlay = NULL; } pthread_create (&progress_t, NULL, Progress_threaD, NULL); Recoveryui::init ();}
1. Gr_init () Initialize the graphics device and allocate the memory rendered by the Pixelflinger library
2. Gr_font_size () assigns the appropriate surface of the font to Char_width and char_height
3, LoadBitmap () Create a PNG surface, each PNG image corresponding to a surface, all surface is stored in an array4. Loadlocalizedbitmap () extracts the text information in the picture that contains the area text according to the current locale, creating the corresponding surface, so surface is also stored in an array6, Pthread_create (&progress_t, NULL, progress_thread, NULL)To create a thread, the task of that thread is a dead loop, which is kept in the loop Currenticon and Progressbartype to decide whether to update the progress bar. 7. Call Init () of Recoveryui. Initializes input event handling.
void Screenrecoveryui::setlocale (const char* locale) { if (locale) { char* lang = strdup (locale); for (char* p = lang; *p; ++p) { if (*p = = ' _ ') { *p = ' + '); break; } } A bit Cheesy:keep an explicit list of supported languages //is RTL. if (strcmp (lang, "ar") = = 0 | | Arabic strcmp (lang, "fa") = = 0 | | Persian (Farsi) strcmp (lang, "he") = = 0 | | Hebrew (new language code) strcmp (lang, "iw") = = 0 | | Hebrew (Old language code) strcmp (lang, "ur") = = 0) { //Urdu Rtl_locale = true; } Free (lang); }}
The setlocale of the Screenrecoveryui class, which infers whether the font used is part of the Arabic language, based on locale. The Arabic writing habit is from right to left, assuming ArabLanguage, a flag is set and the text or progress bar is displayed from right to left based on this flag.
The setlocale's parameter locale assignment logic is this, which is read from the command file, and the commands in the order file set locale as "--locale=zh_cn". Assuming there is no incoming locale,During initialization, the locale is attempted to be read from/cache/recovery/last_locale, and if the file is not, the locale is not assigned a value. It is the default with 中文版.
void Screenrecoveryui::setbackground (icon icon) { pthread_mutex_lock (&updatemutex); Adjust the offset to account for the positioning of the //base image in the screen. if (Backgroundicon[icon]! = NULL) { gr_surface bg = Backgroundicon[icon]; Gr_surface text = Backgroundtext[icon]; overlay_offset_x = install_overlay_offset_x + (gr_fb_width ()-gr_get_width (BG))/2; overlay_offset_y = install_overlay_offset_y + (Gr_fb_height ()-(Gr_get_height (BG) + gr_get_height (text) + 40))/2;< c9/>} Currenticon = icon; Update_screen_locked (); Pthread_mutex_unlock (&updatemutex);}
SetBackground function is relatively concise, the key part in the update_screen_locked, the following we focus on analysis. update_screen_locked and update_progress_locked are key functions of the UI portion of recovery. Update_screen_locked is used to update the background, update_progress_locked is used to update the progress bar, because the displayed screen will always be updated. So these two functions are called repeatedly in different places.
void screenrecoveryui::update_screen_locked () { draw_screen_locked (); Gr_flip ();}
Update_screen_locked includes two operations, one is to update screen, and the other is to switch between buffer.
void Screenrecoveryui::d raw_screen_locked () {draw_background_locked (Currenticon); Draw_progress_locked (); if (show_text) {setcolor (Text_fill); Gr_fill (0, 0, gr_fb_width (), Gr_fb_height ()); int y = 0; int i = 0; if (show_menu) {setcolor (HEADER); for (; i < menu_top + Menu_items; ++i) {if (i = = menu_top) setcolor (menu); if (i = = Menu_top + Menu_sel) {//Draw the Highlight bar setcolor (MENU_SEL_BG); Gr_fill (0, Y-2, Gr_fb_width (), y+char_height+2); White text of selected item setcolor (MENU_SEL_FG); if (Menu[i][0]) Gr_text (4, Y, Menu[i], 1); SetColor (MENU); } else {if (menu[i][0]) Gr_text (4, Y, menu[i], I < menu_top); } y + = char_height+4; } setcolor (MENU); Y + = 4; Gr_fill (0, Y, gr_fb_width (), y+2); Y + = 4; ++i; } setcolor (LOG); Display from the bottom up, until we hits the top of the//screen, the bottom of the menu, or we ' ve displayed th E//Entire text buffer. int Ty; int row = (text_top+text_rows-1)% Text_rows; for (int ty = Gr_fb_height ()-char_height, count = 0; Ty > Y+2 && count < text_rows; Ty-= char_height, ++count) {Gr_text (4, Ty, Text[row], 0); --row; if (Row < 0) row = text_rows-1; } }}
in the implementation code of the draw_background_locked function, there are several functions beginning with gr_, with functions beginning with gr_ from the Minui library .Minui Library code in the recovery source code under the Minui folder, Minui provides the interface to achieve a graphical depiction and fixed-size text display.
Gr_color (unsigned char r, unsigned char g, unsigned char b, unsigned char a); /* Set the font color * /Gr_fill (int x, int y, int w, int h); /* Fills the rectangular area, which represents the starting coordinates, the size of the rectangular area, respectively */Gr_blit (gr_surface source, int sx, int sy, int w, int h, int dx, int dy); /* Fills the picture specified by source * /
The draw_background_locked function fills the entire render buffer with black, then calculates the length and width of the background surface, the length and width of the text surface,combining the length and width of the FB calculates the coordinates of the background surface and the display of the text surface. The interface with the length and width coordinates can be called Pixelflinger to render on render buffer.
void Screenrecoveryui::d raw_progress_locked () {if (Currenticon = = ERROR) return; if (Currenticon = = Installing_update | | currenticon = = erasing) {draw_install_overlay_locked (installingframe); } if (Progressbartype! = EMPTY) {int iconheight = Gr_get_height (Backgroundicon[installing_update]); int width = gr_get_width (progressbarempty); int height = gr_get_height (progressbarempty); int dx = (gr_fb_width ()-width)/2; int dy = (3*gr_fb_height () + iconheight-2*height)/4; Erase Behind the progress bar (in case this is a progress-only update) gr_color (0, 0, 0, 255); Gr_fill (dx, dy, width, height); if (Progressbartype = = determinate) {Float p = progressscopestart + progress * progressscopesize; int pos = (int) (p * width); if (Rtl_locale) {//Fill the progress bar from right to left. if (pos > 0) {gr_blit (Progressbarfill, Width-pos, 0, pos, height, dx+width-pos, dy); } if (pos < width-1) {gr_blit (progressbarempty, 0, 0, width-pos, height, dx, dy); }} else {//Fill the progress bar from left to right. if (pos > 0) {gr_blit (progressbarfill, 0, 0, pos, height, dx, dy); } if (pos < width-1) {Gr_blit (progressbarempty, POS, 0, Width-pos, height, dx+pos, D y); }}} if (Progressbartype = = indeterminate) {static int frame = 0; Gr_blit (Progressbarindeterminate[frame], 0, 0, width, height, dx, dy); In RTL locales, we run the animation backwards, which//makes the spinner spin the other. if (Rtl_locale) {frame = (frame + indeterminate_frames-1)% Indeterminate_frames; } else {Frame = (frame + 1)% Indeterminate_frames; } } }}
the draw_progress_locked function is similar in principle to the update_screen_locked function. Finally, the surface of the progress bar is output to render buffer,The picture of each scene in the recovery is the overlap of the background, text, and progress bar, with the different surface and surface coordinates. The UI code in the recovery main function has basically been parsed. The last point of the main menu is displayed. is to display the text picture through the interfaces described above, so it is no longer a lot to talk about. overall. Recovery UI display is not very difficult, the application layer calls the Minui library to achieve a graphical depiction and fixed-size text display. The Minui library called the Pixelflinger Library for rendering.
attach a description of the Minui part of the interface, for reference
int gr_init (void); /* Initialize the graphical display, mainly to open the device, allocate memory, initialize some of the parameters */void gr_exit (void); /* Unregister the graphic display, turn off the device and free the memory */int gr_fb_width (void); /* Gets the width of the screen */int gr_fb_height (void); /* Get the height of the screen */Gr_pixel *gr_fb_data (void); /* Gets the address of the display data cache */void gr_flip (void); /* Refresh the contents of the display */void Gr_fb_blank (bool blank); /* Clear screen */void Gr_color (unsigned char r, unsigned char g, unsigned char b, unsigned char a); /* Set the font color */void Gr_fill (int x, int y, int w, int h); /* Fills the rectangular area, representing the starting coordinates, the size of the rectangular region */int Gr_text (int x, int y, const char *s), respectively; /* Display String */int gr_measure (const char *s); /* Gets the pixel length that the string occupies in the default font */void gr_font_size (int *x, int *y); /* Gets the current font of a character occupied by the length of */void Gr_blit (gr_surface source, int sx, int sy, int w, int h, int dx, int dy); /* Fills the picture specified by source */unsigned int gr_get_width (gr_surface surface); /* Get picture Width */unsigned int gr_get_height (gr_surface surface); /* Get Picture Height */* Create display resource data based on picture, name for picture relative path specified in MK file */int Res_creaTe_surface (const char* name, gr_surface* psurface); void Res_free_surface (gr_surface surface); /* Release Resource Data */
Android Recovery UI Implementation analysis