編輯:關於 Android 程式設計
Recovery 和主系統其實是兩份獨立的 rootfs,只是 Recovery 存在的唯一目的就是替 Android 服務,所以一般把它視為系統的一部分。正因為它是一個極簡的 rootfs,功能被砍到只剩幾個,UI 也不是走 SurfaceFlinger 那套,而是直接刷 framebuffer——這對習慣 framework 與 app 層開發的人來說相當陌生,值得花點時間梳理。
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);
......
}
幾個關鍵步驟:
GetUI() 回傳一個 DefaultUI 物件。Recovery 裡有三個 UI 類,構成一條繼承鏈:RecoveryUI ← ScreenRecoveryUI ← DefaultUI。Init()。DefaultUI 自己沒有實作 Init,於是實際走的是父類 ScreenRecoveryUI 的版本。SetLocale() 與 SetBackground() 同理,也都落在 ScreenRecoveryUI 上。
void ScreenRecoveryUI::Init() {
gr_init();
gr_font_size(&char_width, &char_height);
text_col = text_row = 0;
text_rows = gr_fb_height() / char_height;
text_top = 1;
text_cols = gr_fb_width() / char_width;
LoadBitmap(icon_installing, &backgroundIcon[INSTALLING_UPDATE]);
LoadBitmap(icon_error, &backgroundIcon[ERROR]);
LoadBitmap(progress_empty, &progressBarEmpty);
LoadBitmap(progress_fill, &progressBarFill);
LoadLocalizedBitmap(installing_text, &backgroundText[INSTALLING_UPDATE]);
......
pthread_create(&progress_t, NULL, progress_thread, NULL);
}
逐項解讀:
gr_init():初始化圖形裝置,並為 Pixelflinger 配置渲染所需記憶體。gr_font_size():把字型 surface 的寬高寫回 char_width/char_height,後續才能算出螢幕可容納幾行幾列文字。LoadBitmap():把一張 png 變成一個 surface,所有圖片 surface 集中放在同一個陣列裡管理。LoadLocalizedBitmap():從含文字的大圖中,依當前 locale 裁出對應語言的那塊區域,同樣生成 surface。pthread_create():開一條專門的執行緒跑無窮迴圈,持續檢查 currentIcon 與 progressBarType,決定要不要更新進度條。Init(),把輸入事件處理初始化起來。
void ScreenRecoveryUI::SetLocale(const char* locale) {
if (locale) {
char* lang = strdup(locale);
for (char* p = lang; *p; ++p) {
if (*p == '_') { *p = '\0'; break; }
}
// 明確列出支援的 RTL 語言
if (strcmp(lang, "ar") == 0 || // Arabic
strcmp(lang, "fa") == 0 || // Persian
strcmp(lang, "he") == 0 || // Hebrew
strcmp(lang, "ur") == 0) { // Urdu
rtl_locale = true;
}
free(lang);
}
}
這個函數判斷當前語言是否屬於阿拉伯語系。這類語言的書寫方向是由右到左,所以一旦命中就設一個旗標,後續畫文字和進度條時依旗標決定要不要反向填色。至於 locale 從哪來?優先讀 command 檔裡的 --locale=zh_CN;若沒有,初始化流程會嘗試讀 /cache/recovery/last_locale;兩者皆無時就不賦值,也就是預設英文。
void ScreenRecoveryUI::SetBackground(Icon icon) {
pthread_mutex_lock(&updateMutex);
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;
}
currentIcon = icon;
update_screen_locked();
pthread_mutex_unlock(&updateMutex);
}
SetBackground 本身很簡短,重頭戲在它呼叫的 update_screen_locked()。它和 update_progress_locked() 一起構成 Recovery UI 的核心:前者負責更新背景,後者負責更新進度條。由於畫面必須持續反映狀態,這兩個函數會在不同地方被反覆呼叫。
void ScreenRecoveryUI::update_screen_locked() {
draw_screen_locked();
gr_flip(); // 切換前後 buffer
}
可見一次更新包含兩件事:把內容畫上去,然後翻頁顯示。
void ScreenRecoveryUI::draw_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());
......
}
}
draw_background_locked() 裡出現的一堆 gr_ 開頭函數都來自 minui 庫,原始碼就在 recovery 的 minui 目錄下,負責圖形描繪與固定尺寸文字輸出:
gr_color(r, g, b, a); /* 設定顏色 */ gr_fill(x, y, w, h); /* 填滿矩形區域 */ gr_blit(source, sx, sy, w, h, dx, dy); /* 貼上 source 指定的圖片 */
它的做法很直白:先把整個渲染 buffer 填成黑色,接著算出背景 surface 與文字 surface 的長寬,再結合 framebuffer 的長寬推出各自的繪製座標,有了尺寸和座標就能呼叫 Pixelflinger 的介面把像素寫進 buffer。
void ScreenRecoveryUI::draw_progress_locked() {
if (currentIcon == ERROR) return;
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;
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) { // 右到左語言反向填色
gr_blit(progressBarFill, width - pos, 0, pos, height,
dx + width - pos, dy);
} else {
gr_blit(progressBarFill, 0, 0, pos, height, dx, dy);
}
}
}
}
原理和 update_screen_locked() 一模一樣,差別只在用的 surface 和座標不同。Recovery 各個情境的畫面,本質上就是「背景 + 文字 + 進度條」這幾張 surface 的疊加組合。
int gr_init(void); /* 開啟裝置、配置記憶體、初始化參數 */ void gr_exit(void); /* 關閉裝置並釋放記憶體 */ int gr_fb_width(void); /* 螢幕寬度 */ int gr_fb_height(void); /* 螢幕高度 */ gr_pixel *gr_fb_data(void); /* 顯示資料緩衝區位址 */ void gr_flip(void); /* 刷新顯示 */ void gr_fb_blank(bool blank); /* 清屏 */ void gr_color(r, g, b, a); /* 設定顏色 */ void gr_fill(int x, int y, int w, int h); /* 填滿矩形 */ int gr_text(int x, int y, const char *s); /* 顯示字串 */ int gr_measure(const char *s); /* 字串佔多少像素寬 */ void gr_font_size(int *x, int *y); /* 單字符長寬 */ void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy); unsigned int gr_get_width(gr_surface surface); unsigned int gr_get_height(gr_surface surface); int res_create_surface(const char* name, gr_surface* pSurface); void res_free_surface(gr_surface surface);
整體看下來,Recovery 的 UI 部分難度並不高:應用層呼叫 minui 完成圖形與文字繪製,minui 再往下呼叫 Pixelflinger 做真正的渲染。之所以看起來陌生,只是因為它跳過了 Android 平時那一整套視圖體系,直接把像素推到 framebuffer 上而已。
原始碼剖析:Volley 的架構、執行流程與快取機制
Volley 是一個 HTTP 庫,能讓 Android App 更輕鬆地完成網路操作,而它最大的賣點是快且有效率。原始碼可以直接從 AOSP 倉庫取得,主要優點包括:自
Android RecyclerView 瀑布流添加 Footer 實現上拉加載
這篇拖了幾個月才寫,中途卡在 Footer 顯示的問題上折騰了很久。起因是朋友問我Android 裡怎麼實現瀑布流佈局,我說用 RecyclerView;他又問那怎麼做上
Android 系統內建螢幕錄製:用 screenrecord 錄成 MP4
不管是做教學還是產品演示,能把 Android 手機或平板的螢幕錄成影片,都是件很實用的事。早期只能靠連續截圖再拼成影片,效果極差。從 Android 4.4(KitKa
Android 學習系列之一:用兩個按鈕實現顯示日期與時間
作為 Android 學習的入門實作,本文將帶大家用 Android Studio 建立一個簡單的專案:透過兩個按鈕分別顯示目前的日期與時間。 首先新建專案並選擇空白模板