Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.4 SystemUI 源碼分析(一):從 SystemUIService 到 PowerUI

編輯:關於 Android 程式設計

以下分析基於 MTK 的 Android 4.4 原生 SystemUI。MTK 版本與 Google 原生版本有細微差異,但整體架構是一致的。

這是 SystemUI 系列的第一篇,先從最簡單的 PowerUI 著手。源碼路徑:/frameworks/base/packages/SystemUI,程式目錄結構如下:

匯入 Eclipse 後會出現大量錯誤,原因多半是找不到 framework 的內部類別與資源(例如 com.android.internal.R)——這是正常的,因為這些符號只有在平台完整原始碼樹中才存在。修改完之後用 mmm 編譯成模組,再 push 到 eng 版本的手機上除錯,push 後需重啟才會生效。

一、SystemUI 沒有啟動器圖示,它是被外部拉起的

在 AndroidManifest.xml 中找不到任何 Activity 註冊:

<intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

所以整個程式是被外部啟動的,具體來說是透過 SystemUIService。而拉起它的地方在:

/frameworks/base/services/java/com/android/server/SystemServer.java

static final void startSystemUi(Context context) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName(
            "com.android.systemui",
            "com.android.systemui.SystemUIService"));
    context.startServiceAsUser(intent, UserHandle.OWNER);
}

也就是說,SystemUI 在 SystemServer 啟動過程中就被拉起來了,它的生命週期與系統本身綁在一起。

二、SystemUIService:一個負責啟動一堆子服務的容器

package com.android.systemui;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.util.HashMap;

public class SystemUIService extends Service {

    private static final String TAG = "SystemUIService";

    /** 要啟動的元件清單 */
    private final Class<?>[] SERVICES = new Class[] {
            com.android.systemui.recent.Recents.class,
            com.android.systemui.statusbar.SystemBars.class,
            com.android.systemui.usb.StorageNotification.class,
            com.android.systemui.power.PowerUI.class,
            com.android.systemui.media.RingtonePlayer.class,
            com.android.systemui.settings.SettingsUI.class,
    };

    /** 持有我們啟動的這些元件的參考 */
    private final SystemUI[] mServices = new SystemUI[SERVICES.length];

    @Override
    public void onCreate() {
        HashMap<Class<?>, Object> components = new HashMap<Class<?>, Object>();
        final int N = SERVICES.length;

        for (int i = 0; i < N; i++) {
            Class<?> cl = SERVICES[i];
            Log.d(TAG, "loading: " + cl);
            try {
                mServices[i] = (SystemUI) cl.newInstance();
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            } catch (InstantiationException ex) {
                throw new RuntimeException(ex);
            }
            mServices[i].mContext = this;
            mServices[i].mComponents = components;
            Log.d(TAG, "running: " + mServices[i]);
            mServices[i].start();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

這個設計很值得注意:清單裡的 Recents、SystemBars、StorageNotification、PowerUI、RingtonePlayer、SettingsUI 全都繼承自 SystemUI,在 onCreate() 中被逐一建立實例並呼叫 start()。

另外 mComponents 這個共用 HashMap 是元件間溝通的通道——例如 StorageNotification 需要存取通知管理器時,可以從這個表裡取得共享實例,避免各自 new 一份。

三、PowerUI:最單純的起點

接著看最簡單的 com.android.systemui.power.PowerUI。它的 start() 方法主要在做一件事:註冊監聽器。

public void start() {
    mLowBatteryAlertCloseLevel = mContext.getResources().getInteger(
            com.android.internal.R.integer.config_lowBatteryCloseWarningLevel);

    mLowBatteryReminderLevels[0] = mContext.getResources().getInteger(
            com.android.internal.R.integer.config_lowBatteryWarningLevel);
    mLowBatteryReminderLevels[1] = mContext.getResources().getInteger(
            com.android.internal.R.integer.config_criticalBatteryWarningLevel);

    final PowerManager pm = (PowerManager)
            mContext.getSystemService(Context.POWER_SERVICE);
    mScreenOffTime = pm.isScreenOn() ? -1 : SystemClock.elapsedRealtime();

    // 註冊廣播意圖
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);

    // M: 設定變更時顯示電量
    filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);

    // M: PowerOffAlarm 響鈴時隱藏低電量對話框
    filter.addAction("android.intent.action.normal.boot");
    filter.addAction("android.intent.action.ACTION_SHUTDOWN_IPO");

    mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
}

邏輯非常直白:透過 ACTION_BATTERY_CHANGED 判斷電量高低,低於門檻時給使用者提示;其他的 ACTION 則各自對應特定的處理。門檻值不是寫死的,而是從 com.android.internal.R.integer 讀取,這也是為什麼 OEM 可以只改資源檔就調整行為,而不必動 Java 程式碼。

實務結論:如果跟電源相關的功能需要客製化或新增監聽項目,這個類別就是修改的入口。想知道 SystemUI 怎麼啟動、有哪些子模組、電量提示怎麼觸發,從這裡往下讀最有效率。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved