引言
Quick Settings行內人簡稱qs,是SystemUI必不可少的一部分。它主要負責打開關閉各個系統(tǒng)的功能模塊,如wifi、藍牙、手電筒等。

從圖中可以看出,每一個系統(tǒng)功能都對應這一個按鈕,而這個按鈕的學名就是Tile。
正文
單個Tile的加載流程
SystemUI是如何加載每個Tile的呢?在Statusbar初始化的時候,在makeStatusbarBarView()中對QsTile進行了初始化:
protected void makeStatusBarView() {
......
// Set up the quick settings tile panel
View container = mStatusBarWindow.findViewById(R.id.qs_frame);
if (container != null) {
FragmentHostManager fragmentHostManager = FragmentHostManager.get(container);
ExtensionFragmentListener.attachExtensonToFragment(container, QS.TAG, R.id.qs_frame,
Dependency.get(ExtensionController.class)
.newExtension(QS.class)
.withPlugin(QS.class)
.withFeature(PackageManager.FEATURE_AUTOMOTIVE, CarQSFragment::new)
.withDefault(QSFragment::new)
.build());
final QSTileHost qsh = SystemUIFactory.getInstance().createQSTileHost(mContext, this,
mIconController);
mBrightnessMirrorController = new BrightnessMirrorController(mStatusBarWindow,
(visible) -> {
mBrightnessMirrorVisible = visible;
updateScrimController();
});
fragmentHostManager.addTagListener(QS.TAG, (tag, f) -> {
QS qs = (QS) f;
if (qs instanceof QSFragment) {
((QSFragment) qs).setHost(qsh);
mQSPanel = ((QSFragment) qs).getQsPanel();
mQSPanel.setBrightnessMirror(mBrightnessMirrorController);
mKeyguardStatusBar.setQSPanel(mQSPanel);
}
});
}
......
}
通過初始化QsTileHost,進行QsTile的加載。在QsTileHost初始化的時候,增加Tunable監(jiān)聽:
public QSTileHost(Context context, StatusBar statusBar,
StatusBarIconController iconController) {
mIconController = iconController;
mContext = context;
mStatusBar = statusBar;
mServices = new TileServices(this, Dependency.get(Dependency.BG_LOOPER));
mQsFactories.add(new QSFactoryImpl(this));
Dependency.get(PluginManager.class).addPluginListener(this, QSFactory.class, true);
Dependency.get(TunerService.class).addTunable(this, TILES_SETTING);
// AutoTileManager can modify mTiles so make sure mTiles has already been initialized.
mAutoTiles = new AutoTileManager(context, this);
}
這里的TunerSerivce的作用是注冊對字段"sysui_qs_tiles"的系統(tǒng)數(shù)據(jù)庫監(jiān)聽:
private void addTunable(Tunable tunable, String key) {
if (!mTunableLookup.containsKey(key)) {
mTunableLookup.put(key, new ArraySet<Tunable>());
}
mTunableLookup.get(key).add(tunable);
if (LeakDetector.ENABLED) {
mTunables.add(tunable);
Dependency.get(LeakDetector.class).trackCollection(mTunables, "TunerService.mTunables");
}
Uri uri = Settings.Secure.getUriFor(key);
if (!mListeningUris.containsKey(uri)) {
mListeningUris.put(uri, key);
mContentResolver.registerContentObserver(uri, false, mObserver, mCurrentUser);
}
// Send the first state.
String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser);
tunable.onTuningChanged(key, value);
}
通過addTunable觸發(fā)QsTileHost中onTuningChanged:
@Override
public void onTuningChanged(String key, String newValue) {
if (!TILES_SETTING.equals(key)) {
return;
}
if (DEBUG) Log.d(TAG, "Recreating tiles");
if (newValue == null && UserManager.isDeviceInDemoMode(mContext)) {
newValue = mContext.getResources().getString(R.string.quick_settings_tiles_retail_mode);
}
final List<String> tileSpecs = loadTileSpecs(mContext, newValue);
int currentUser = ActivityManager.getCurrentUser();
if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return;
mTiles.entrySet().stream().filter(tile -> !tileSpecs.contains(tile.getKey())).forEach(
tile -> {
if (DEBUG) Log.d(TAG, "Destroying tile: " + tile.getKey());
tile.getValue().destroy();
});
final LinkedHashMap<String, QSTile> newTiles = new LinkedHashMap<>();
for (String tileSpec : tileSpecs) {
QSTile tile = mTiles.get(tileSpec);
if (tile != null && (!(tile instanceof CustomTile)
|| ((CustomTile) tile).getUser() == currentUser)) {
if (tile.isAvailable()) {
if (DEBUG) Log.d(TAG, "Adding " + tile);
tile.removeCallbacks();
if (!(tile instanceof CustomTile) && mCurrentUser != currentUser) {
tile.userSwitch(currentUser);
}
newTiles.put(tileSpec, tile);
} else {
tile.destroy();
}
} else {
if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec);
try {
tile = createTile(tileSpec);
if (tile != null) {
if (tile.isAvailable()) {
tile.setTileSpec(tileSpec);
newTiles.put(tileSpec, tile);
} else {
tile.destroy();
}
}
} catch (Throwable t) {
Log.w(TAG, "Error creating tile for spec: " + tileSpec, t);
}
}
}
mCurrentUser = currentUser;
mTileSpecs.clear();
mTileSpecs.addAll(tileSpecs);
mTiles.clear();
mTiles.putAll(newTiles);
for (int i = 0; i < mCallbacks.size(); i++) {
mCallbacks.get(i).onTilesChanged();
}
}
以上步驟進行了讀取配置,并creatTile的操作。通過loadTileSpecs()獲取到需要createTile的list:
protected List<String> loadTileSpecs(Context context, String tileList) {
final Resources res = context.getResources();
String defaultTileList = res.getString(R.string.quick_settings_tiles_default);
if (tileList == null) {
tileList = res.getString(R.string.quick_settings_tiles);
if (DEBUG) Log.d(TAG, "Loaded tile specs from config: " + tileList);
} else {
if (DEBUG) Log.d(TAG, "Loaded tile specs from setting: " + tileList);
}
final ArrayList<String> tiles = new ArrayList<String>();
boolean addedDefault = false;
for (String tile : tileList.split(",")) {
tile = tile.trim();
if (tile.isEmpty()) continue;
if (tile.equals("default")) {
if (!addedDefault) {
tiles.addAll(Arrays.asList(defaultTileList.split(",")));
addedDefault = true;
}
} else {
tiles.add(tile);
}
}
return tiles;
}
通過"quick_settings_tiles_default"進入默認tile的配置,這里的tile list不包括可編輯的。獲取到list之后,通過creatTile()進行創(chuàng)建:
public QSTile createTile(String tileSpec) {
for (int i = 0; i < mQsFactories.size(); i++) {
QSTile t = mQsFactories.get(i).createTile(tileSpec);
if (t != null) {
return t;
}
}
return null;
}
這里通過循環(huán),在QsFactoryImpl中進行tile的創(chuàng)建:
public QSTile createTile(String tileSpec) {
QSTileImpl tile = createTileInternal(tileSpec);
if (tile != null) {
tile.handleStale(); // Tile was just created, must be stale.
}
return tile;
}
private QSTileImpl createTileInternal(String tileSpec) {
switch (tileSpec) {
case "wifi":
return new WifiTile(mHost);
case "bt":
return new BluetoothTile(mHost);
case "cell":
return new CellularTile(mHost);
case "dnd":
return new DndTile(mHost);
case "inversion":
return new ColorInversionTile(mHost);
case "airplane":
return new AirplaneModeTile(mHost);
case "work":
return new WorkModeTile(mHost);
case "rotation":
return new RotationLockTile(mHost);
case "flashlight":
return new FlashlightTile(mHost);
case "location":
return new LocationTile(mHost);
case "cast":
return new CastTile(mHost);
case "hotspot":
return new HotspotTile(mHost);
case "user":
return new UserTile(mHost);
case "battery":
return new BatterySaverTile(mHost);
case "saver":
return new DataSaverTile(mHost);
case "night":
return new NightDisplayTile(mHost);
case "nfc":
return new NfcTile(mHost);
}
// Intent tiles.
if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(mHost, tileSpec);
if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(mHost, tileSpec);
// Debug tiles.
if (Build.IS_DEBUGGABLE) {
if (tileSpec.equals(GarbageMonitor.MemoryTile.TILE_SPEC)) {
return new GarbageMonitor.MemoryTile(mHost);
}
}
// Broken tiles.
Log.w(TAG, "Bad tile spec: " + tileSpec);
return null;
}
根據(jù)tileSpec進行對應tile的初始化。
編輯區(qū)Tile加載流程
當用戶點擊qs區(qū)域的編輯按鈕時,可以看到一個這樣的界面:


從圖中可以看出,進入qs編輯模式之后,會出現(xiàn)更多tile,并且可以拖動到上方供用戶平時下拉后直接使用,或者將不常用的放到待選區(qū)域。這里從UI上可以看出待選區(qū)域由兩部分組成,一部分是SystemUI內部的Tile,而類似google Nearby這種Tile則是第三應用自己注冊的Tile。接下來,就分析一下這些Tile是如何加載的?
當進入編輯模式之后,QsCustomer會對tile有一個query操作,由TileQueryHelper實現(xiàn):
public void queryTiles(QSTileHost host) {
mTiles.clear();
mSpecs.clear();
mFinished = false;
// Enqueue jobs to fetch every system tile and then ever package tile.
addStockTiles(host);
addPackageTiles(host);
}
從代碼中也可以看出,加載Tile分成了StockTiles和PackageTiles,分別代表SystemUI內置Tile和第三注冊Tile,先來看下內置Tile:
private void addStockTiles(QSTileHost host) {
String possible = mContext.getString(R.string.quick_settings_tiles_stock);
final ArrayList<String> possibleTiles = new ArrayList<>();
possibleTiles.addAll(Arrays.asList(possible.split(",")));
if (Build.IS_DEBUGGABLE) {
possibleTiles.add(GarbageMonitor.MemoryTile.TILE_SPEC);
}
final ArrayList<QSTile> tilesToAdd = new ArrayList<>();
for (String spec : possibleTiles) {
final QSTile tile = host.createTile(spec);
if (tile == null) {
continue;
} else if (!tile.isAvailable()) {
tile.destroy();
continue;
}
tile.setListening(this, true);
tile.clearState();
tile.refreshState();
tile.setListening(this, false);
tile.setTileSpec(spec);
tilesToAdd.add(tile);
}
mBgHandler.post(() -> {
for (QSTile tile : tilesToAdd) {
final QSTile.State state = tile.getState().copy();
// Ignore the current state and get the generic label instead.
state.label = tile.getTileLabel();
tile.destroy();
addTile(tile.getTileSpec(), null, state, true);
}
notifyTilesChanged(false);
});
}
"quick_settings_tiles_stock"對應的就是SystemUI中可加載的所有Tile,如果希望客制化也可以在此處進行修改。接下來,再看一下如何加載第三方Tile(如Nearby):
private void addPackageTiles(final QSTileHost host) {
mBgHandler.post(() -> {
Collection<QSTile> params = host.getTiles();
PackageManager pm = mContext.getPackageManager();
List<ResolveInfo> services = pm.queryIntentServicesAsUser(
new Intent(TileService.ACTION_QS_TILE), 0, ActivityManager.getCurrentUser());
String stockTiles = mContext.getString(R.string.quick_settings_tiles_stock);
for (ResolveInfo info : services) {
String packageName = info.serviceInfo.packageName;
ComponentName componentName = new ComponentName(packageName, info.serviceInfo.name);
// Don't include apps that are a part of the default tile set.
if (stockTiles.contains(componentName.flattenToString())) {
continue;
}
final CharSequence appLabel = info.serviceInfo.applicationInfo.loadLabel(pm);
String spec = CustomTile.toSpec(componentName);
State state = getState(params, spec);
if (state != null) {
addTile(spec, appLabel, state, false);
continue;
}
if (info.serviceInfo.icon == 0 && info.serviceInfo.applicationInfo.icon == 0) {
continue;
}
Drawable icon = info.serviceInfo.loadIcon(pm);
if (!permission.BIND_QUICK_SETTINGS_TILE.equals(info.serviceInfo.permission)) {
continue;
}
if (icon == null) {
continue;
}
icon.mutate();
icon.setTint(mContext.getColor(android.R.color.white));
CharSequence label = info.serviceInfo.loadLabel(pm);
addTile(spec, icon, label != null ? label.toString() : "null", appLabel);
}
notifyTilesChanged(true);
});
}
從代碼中不難看出,通過PackageManager來query帶有“TileService.ACTION_QS_TILE”的Intent的應用,獲取到它們的resolveinfo獲取第三方注冊的Tile的信息,如Tile的名稱、icon、應用名稱等。
到這里,Qs Tile加載的數(shù)據(jù)流程已經(jīng)講完,后面有時間再講一下UI的加載流程。如有什么問題歡迎指正。
本文章已經(jīng)獨家授權ApeClub公眾號使用。