What is Android recovery mode?
on this issue, Baidu has countless answers, do not understand the friend first tutorial. From a purely technical point of view, recovery and Android are essentially two separate rootfs, but recovery this rootfs existence is meant to serve the rootfs of Android and is therefore interpreted as part of the Android system. recovery as a simple rootfs, provides a very limited number of features, contains only a few simple libraries, the UI is displayed in the form of direct brush framebuffer, as the Android framework and the app layer of the Code farm, Relatively unfamiliar with this form, 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 object of a device class is created first, and the device class encapsulates some 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 Screenrecoveryui class of setlocale () to identify a few more specific areas5, similarly, call the Screenrecoveryui class of SetBackground () 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 surface width of the font to Char_width and char_height
3. LoadBitmap () creates a PNG surface with one surface for each PNG image, and all surface is stored in an array4. Loadlocalizedbitmap () extracts the text information from the picture containing the area text according to the current locale to generate 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 To detect Currenticon and Progressbartype to determine whether to update the progress bar. 7. Call Recoveryui init () to initialize the input event processing.
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 determines whether the font used by locale belongs to the Arabic language, the Arabic writing habit is from right to left, if the Arablanguage, set a flag, followed by this flag to determine the right-to-left display text or progress bar. The parameter locale assignment logic for the setlocale is this, which is read from the command file, where the commands for locale are set as "--locale=zh_cn", if there is no incoming locale, The locale is attempted to be read from the/cache/recovery/last_locale during initialization, and if the file is not, the locale is not assigned, and it is used by default.
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 update_screen_locked, below we focus on analysis. update_screen_locked and update_progress_locked are key functions of the UI part of recovery, update_screen_locked is used to update the background, Update_progress_ The locked is used to update the progress bar because the displayed screen is always updated, so the two functions are called repeatedly in different places
void screenrecoveryui::update_screen_locked () { draw_screen_locked (); Gr_flip ();}
Update_screen_locked contains 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 under the Minui directory, 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, with the parameters representing the starting coordinates, the rectangular area size */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,combined with the long width of the FB to calculate the background surface and the coordinates of the text surface display, there is a long width and coordinates can call the Pixelflinger interface on render buffer to render.
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 to the update_screen_locked function, and ultimately outputs the progress bar's surface 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 been basically analyzed, and the last point of the main menu is to display the text picture through the interfaces described above, so it's no longer a lot to talk about. in general, recovery UI display part of the difficulty, the application layer call Minui Library to achieve the graphics and fixed-size text display, Minui library called the Pixelflinger Library to render. 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 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, with the parameters representing the starting coordinates, the rectangular area size */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 according to 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