systemUI 下拉状态栏快捷开关是 QSPanel,qs_panel.xml,@+ID/quick_settings_panel,本篇文章就来看看这些快捷开关是如何呈现的以及如何新增一个快捷开关?基于 AOSP 9.0 分析。
systemUI 下拉状态栏快捷开关QSPanel 创建是从 Statusbar#makeStatusbarVIEw 开始的。
Statusbar#makeStatusbarVIEwprotected voID makeStatusbarVIEw() {
//省略其他代码
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);
}
});
//省略其他代码
}
先看 systemUIFactory#createQSTileHost。
systemUIFactory#createQSTileHostpublic QSTileHost createQSTileHost(Context context, Statusbar statusbar,
StatusbarIconController iconController) {
return new QSTileHost(context, statusbar, iconController);
}
这里进行 QSTileHost 初始化。
QSTileHost#构造函数public QSTileHost(Context context, Statusbar statusbar,
StatusbarIconController iconController) {
//省略其他代码
Dependency.get(TunerService.class).addTunable(this, TILES_SETTING);
//省略其他代码
}
这里进行了 TunerService 注册,在 TunerServiceImpl#addTunable 重写。
TunerServiceImpl#addTunable@OverrIDe
public voID addTunable(Tunable tunable, String... keys) {
for (String key : keys) {
addTunable(tunable, key);
}
}
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);
}
tunable.onTuningChanged
回调 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);
}
//调用 QSTileHost#loadTileSpecs,获得 config 里字符串信息
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
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();
}
}
看下 QSTileHost#loadTileSpecs,是获得 config 里字符串信息。
QSTileHost#loadTileSpecsprotected List<String> loadTileSpecs(Context context, String tileList) {
final Resources res = context.getResources();
final 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 值在 AOSP/frameworks/base/packages/systemUI/res/values/config.xml 里:
<string name="quick_settings_tiles_default" translatable="false">
wifi,bt,dnd,flashlight,rotation,battery,cell,airplane,cast
</string>
这里就是我们所看到的快捷开关的文本描述。
再看 QSTileHost#onTuningChanged 中的调用 QSTileHost#createTile 方法。
QSTileHost#createTilepublic 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;
}
调用 QSFactory#createTile,由 QSFactoryImpl#createTile 实现了。
QSFactoryImpl#createTilepublic 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) {
// Stock tiles.
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;
}
看到这里通过对应的字符串分别实例化 Tile。
以上涉及资源文件加载及对应实例化,接下来看看代码如何加载的,看 QSPanel#onAttachedToWindow 方法。
QSPanel#onAttachedToWindow@OverrIDe
protected voID onAttachedToWindow() {
super.onAttachedToWindow();
final TunerService tunerService = Dependency.get(TunerService.class);
tunerService.addTunable(this, QS_SHOW_BRIGHTnesS);
if (mHost != null) {
setTiles(mHost.getTiles());
}
if (mBrightnessMirrorController != null) {
mBrightnessMirrorController.addCallback(this);
}
}
public voID setTiles(Collection<QSTile> tiles) {
setTiles(tiles, false);
}
public voID setTiles(Collection<QSTile> tiles, boolean collapsedVIEw) {
if (!collapsedVIEw) {
mQsTileRevealController.updateRevealedTiles(tiles);
}
for (TileRecord record : mRecords) {
mTileLayout.removeTile(record);
record.tile.removeCallback(record.callback);
}
mRecords.clear();
for (QSTile tile : tiles) {
addTile(tile, collapsedVIEw);
}
}
protected TileRecord addTile(final QSTile tile, boolean collapsedVIEw) {
final TileRecord r = new TileRecord();
r.tile = tile;
r.tileVIEw = createTileVIEw(tile, collapsedVIEw);
//省略其他代码
r.tileVIEw.init(r.tile);
r.tile.refreshState();
mRecords.add(r);
if (mTileLayout != null) {
mTileLayout.addTile(r);
}
return r;
}
mTileLayout.addTile(r);由 PagedTileLayout#addTile 实现。
PagedTileLayout#addTilePagedTileLayout 是 VIEwPager,重点看 setAdapter,看数据源如何 add 的。
@OverrIDe
public voID addTile(TileRecord tile) {
mTiles.add(tile);
postdistributeTiles();
}
private voID postdistributeTiles() {
removeCallbacks(mdistribute);
post(mdistribute);
}
private final Runnable mdistribute = new Runnable() {
@OverrIDe
public voID run() {
distributeTiles();
}
};
private voID distributeTiles() {
if (DEBUG) Log.d(TAG, "distributing tiles");
final int NP = mPages.size();
for (int i = 0; i < NP; i++) {
mPages.get(i).removeAllVIEws();
}
int index = 0;
final int NT = mTiles.size();
for (int i = 0; i < NT; i++) {
TileRecord tile = mTiles.get(i);
if (mPages.get(index).isFull()) {
if (++index == mPages.size()) {
if (DEBUG) Log.d(TAG, "Adding page for "
+ tile.tile.getClass().getSimplename());
mPages.add((TilePage) LayoutInflater.from(getContext())
.inflate(R.layout.qs_paged_page, this, false));
}
}
if (DEBUG) Log.d(TAG, "Adding " + tile.tile.getClass().getSimplename() + " to "
+ index);
mPages.get(index).addTile(tile);
}
if (mNumPages != index + 1) {
mNumPages = index + 1;
while (mPages.size() > mNumPages) {
mPages.remove(mPages.size() - 1);
}
if (DEBUG) Log.d(TAG, "Size: " + mNumPages);
mPageIndicator.setNumPages(mNumPages);
setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
setCurrentItem(0, false);
}
}
至此,systemUI 下拉状态栏快捷开关模块代码流程分析完毕。
新增一个快捷开关0、国际惯例,先上效果图,新增一个Camera,随便用了蓝牙的图标:
@H_278_419@
1、首先在 AOSP/frameworks/base/packages/systemUI/res/values/config.xml 里面添加截屏 Camera 的选项
<string name="quick_settings_tiles_default" translatable="false">
wifi,bt,dnd,flashlight,rotation,battery,cell,airplane,cast,camera
</string>
2、在 AOSP/frameworks/base/packages/systemUI/res/values/strings.xml 里面还要加一个字符串
<string name="quick_settings_camera_label">Camera</string>
3、在 AOSP/frameworks/base/packages/systemUI/src/com/androID/systemUI/qs/tiles/ 目录下创建 CameraTile.java,实现 QSTileImpl:
package com.androID.systemUI.qs.tiles;
import androID.content.Intent;
import androID.provIDer.MediaStore;
import androID.Widget.Toast;
//手动添加
import com.androID.systemUI.R;
import com.androID.systemUI.plugins.qs.QSTile;
import com.androID.systemUI.qs.QSHost;
import com.androID.systemUI.qs.tileimpl.QSTileImpl;
//手动添加
import com.androID.internal.logging.nano.MetricsProto.MetricsEvent;
public class CameraTile extends QSTileImpl<QSTile.BooleanState> {
public CameraTile(QSHost host) {
super(host);
}
@OverrIDe
public BooleanState newTileState() {
return new BooleanState();
}
@OverrIDe
protected voID handleClick() {
Toast.makeText(mContext,"Camera Click",Toast.LENGTH_LONG).show();
}
@OverrIDe
protected voID handleUpdateState(BooleanState state, Object arg) {
state.label = mContext.getString(R.string.quick_settings_camera_label);
//定义图标,随便用了蓝牙的图标
state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on);
}
@OverrIDe
public int getMetricscategory() {
return MetricsEvent.QS_CAMERA;
}
@OverrIDe
public Intent getLongClickIntent() {
return new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
}
@OverrIDe
protected voID handleSetListening(boolean Listening) {
}
@OverrIDe
public CharSequence getTileLabel() {
return mContext.getString(R.string.quick_settings_camera_label);
}
}
4、在 AOSP/frameworks/base/proto/src/metrics_constants.proto,增加常量:
QS_CAMERA = 1568;
5、在 AOSP/frameworks/base/packages/systemUI/src/com/androID/systemUI/qs/tileimpl/QSFactoryImpl.java,增加:
private QSTileImpl createTileInternal(String tileSpec) {
// Stock tiles.
switch (tileSpec) {
case "wifi":
return new WifiTile(mHost);
// 省略部分代码
case "nfc":
return new NfcTile(mHost);
case "camera":
return new CameraTile(mHost);
}
// 省略部分代码
}
6、整编代码,运行模拟器,有效果,棒棒哒。
总结以上是内存溢出为你收集整理的Android 9.0 SystemUI 下拉状态栏快捷开关全部内容,希望文章能够帮你解决Android 9.0 SystemUI 下拉状态栏快捷开关所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)