1.在config中添加
要添加图标,首先我们需要给我们的图标添加在配置文件config.xml中
<!-- The default tiles to display in QuickSettings -->
<string name="quick_settings_tiles_default" translatable="false">
internet,bt,flashlight,dnd,alarm,airplane,controls,wallet,hotspot,rotation,battery,cast,screenrecord,mictoggle,cameratoggle,custom(com.android.permissioncontroller/.permission.service.SafetyCenterQsTileService)
</string>
要是导入了gms里面有个重写,注意要在那里添加路径一般在
./overlay/gms_overlay/frameworks/base/packages/SystemUI/res/values/config.xml
在mtk平台他有自己实现的SystemUi这个类
路径一般在vendor下
2.实现图标功能
之前的android版本中我们通过QSFactoryImpl实现,这里我们也看下这个类,在android14中通过依赖注入的方式实现的
@SysUISingleton
public class QSFactoryImpl implements QSFactory {
private static final String TAG = "QSFactory";
protected final Map<String, Provider<QSTileImpl<?>>> mTileMap;
private final Lazy<QSHost> mQsHostLazy;
private final Provider<CustomTile.Builder> mCustomTileBuilderProvider;
@Inject
public QSFactoryImpl(
Lazy<QSHost> qsHostLazy,
Provider<CustomTile.Builder> customTileBuilderProvider,
Map<String, Provider<QSTileImpl<?>>> tileMap) {
mQsHostLazy = qsHostLazy;
mCustomTileBuilderProvider = customTileBuilderProvider;
mTileMap = tileMap;
}
/** Creates a tile with a type based on {@code tileSpec} */
@Nullable
public final QSTile createTile(String tileSpec) {
QSTileImpl tile = createTileInternal(tileSpec);
if (tile != null) {
tile.initialize();
tile.postStale(); // Tile was just created, must be stale.
}
return tile;
}
@Nullable
protected QSTileImpl createTileInternal(String tileSpec) {
// Stock tiles.
if (mTileMap.containsKey(tileSpec)
// We should not return a Garbage Monitory Tile if the build is not Debuggable
&& (!tileSpec.equals(GarbageMonitor.MemoryTile.TILE_SPEC) || Build.IS_DEBUGGABLE)) {
return mTileMap.get(tileSpec).get();
}
// Custom tiles
if (tileSpec.startsWith(CustomTile.PREFIX)) {
return CustomTile.create(
mCustomTileBuilderProvider.get(), tileSpec, mQsHostLazy.get().getUserContext());
}
// Broken tiles.
Log.w(TAG, "No stock tile spec: " + tileSpec);
return null;
}
@Override
public QSTileView createTileView(Context context, QSTile tile, boolean collapsedView) {
QSIconView icon = tile.createTileView(context);
return new QSTileViewImpl(context, icon, collapsedView);
}
}
我们看他的构造方法通过依赖注入来得到map,map的数据是如何得到的呢
我们看下.kt文件
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.statusbar.connectivity
import com.android.systemui.qs.tileimpl.QSTileImpl
import com.android.systemui.qs.tiles.AirplaneModeTile
import com.android.systemui.qs.tiles.BluetoothTile
import com.android.systemui.qs.tiles.CastTile
import com.android.systemui.qs.tiles.DataSaverTile
import com.android.systemui.qs.tiles.HotspotTile
import com.android.systemui.qs.tiles.InternetTile
import com.android.systemui.qs.tiles.NfcTile
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
import dagger.multibindings.StringKey
@Module
interface ConnectivityModule {
/** Inject InternetTile into tileMap in QSModule */
@Binds
@IntoMap
@StringKey(InternetTile.TILE_SPEC)
fun bindInternetTile(internetTile: InternetTile): QSTileImpl<*>
/** Inject BluetoothTile into tileMap in QSModule */
@Binds
@IntoMap
@StringKey(BluetoothTile.TILE_SPEC)
fun bindBluetoothTile(bluetoothTile: BluetoothTile): QSTileImpl<*>
/** Inject CastTile into tileMap in QSModule */
@Binds
@IntoMap
@StringKey(CastTile.TILE_SPEC)
fun bindCastTile(castTile: CastTile): QSTileImpl<*>
/** Inject HotspotTile into tileMap in QSModule */
@Binds
@IntoMap
@StringKey(HotspotTile.TILE_SPEC)
fun bindHotspotTile(hotspotTile: HotspotTile): QSTileImpl<*>
/** Inject AirplaneModeTile into tileMap in QSModule */
@Binds
@IntoMap
@StringKey(AirplaneModeTile.TILE_SPEC)
fun bindAirplaneModeTile(airplaneModeTile: AirplaneModeTile): QSTileImpl<*>
/** Inject DataSaverTile into tileMap in QSModule */
@Binds
@IntoMap
@StringKey(DataSaverTile.TILE_SPEC)
fun bindDataSaverTile(dataSaverTile: DataSaverTile): QSTileImpl<*>
/** Inject NfcTile into tileMap in QSModule */
@Binds @IntoMap @StringKey(NfcTile.TILE_SPEC) fun bindNfcTile(nfcTile: NfcTile): QSTileImpl<*>
}
我们看下每个的注解,他都添加了 @IntoMap.之后还添加了key来找到对应的值,
所以我们只需要实现我们的功能,并通过这个方法来将我们的方法添加进去