<string name="quick_settings_tiles_default" translatable="false">
hotspot,location,rotation,work,inversion,cell,nfc,wifi,dnd,bt,flashlight,rotation,battery,cell,airplane,cast,screenshot,browser
</string>
这是默认显示的快捷设置,这里我自定义的快捷设置为browser,将它添加到最后。
<string name="quick_settings_ringer_browser">浏览器</string>
本文直接用NFC的图片资源(ic_qs_nfc_enabled.xml)
新增:
QS_BROWSER = 1146;
注意:metrics_constants.proto文件 修改完成后 mma重新编译生效
因为默认读取数据库的文件,所以想要配置文件生效,将数据库置空,这样会读取配置文件。
protected List<String> loadTileSpecs(Context context, String tileList) {
final Resources res = context.getResources();
tileList = null; //chenzq: 这里修改屏蔽了数据库的默认设置,强制读取配置文件
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);
}
Log.d("chenzq0", 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;
}
在createTile方法中新增返回自定义Tile
注意:别忘了导入自定义文件的包
public QSTile createTile(String tileSpec) {
Log.d("chenzq1", tileSpec);
if (tileSpec.equals("wifi")) return new WifiTile(mHost);
else if (tileSpec.equals("bt")) return new BluetoothTile(mHost);
else if (tileSpec.equals("cell")) return new CellularTile(mHost);
else if (tileSpec.equals("dnd")) return new DndTile(mHost);
else if (tileSpec.equals("inversion")) return new ColorInversionTile(mHost);
else if (tileSpec.equals("airplane")) return new AirplaneModeTile(mHost);
else if (tileSpec.equals("work")) return new WorkModeTile(mHost);
else if (tileSpec.equals("rotation")) return new RotationLockTile(mHost);
else if (tileSpec.equals("flashlight")) return new FlashlightTile(mHost);
else if (tileSpec.equals("location")) return new LocationTile(mHost);
else if (tileSpec.equals("cast")) return new CastTile(mHost);
else if (tileSpec.equals("hotspot")) return new HotspotTile(mHost);
else if (tileSpec.equals("user")) return new UserTile(mHost);
else if (tileSpec.equals("battery")) return new BatterySaverTile(mHost);
else if (tileSpec.equals("saver")) return new DataSaverTile(mHost);
else if (tileSpec.equals("night")) return new NightDisplayTile(mHost);
else if (tileSpec.equals("nfc")) return new NfcTile(mHost);
else if (tileSpec.equals("browser")) return new BrowserTile(mHost);//新增自定义Tile
// Intent tiles.
else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(mHost, tileSpec);
else if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(mHost, tileSpec);
else if (tileSpec.equals("screenshot")) return new ScreenShotTile(mHost);
else {
Log.w(TAG, "Bad tile spec: " + tileSpec);
return null;
}
}
com.android.systemui.qs.tiles目录下新建BrowserTile.java
要继承QSTileImpl类
package com.android.systemui.qs.tiles;
import android.content.Intent;
import android.util.Log;
import android.service.quicksettings.Tile;
import com.android.systemui.R;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import com.android.systemui.qs.QSHost;
import com.android.systemui.plugins.qs.QSTile;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
public class BrowserTile extends QSTileImpl<BooleanState>{
public BrowserTile(QSHost host) {
super(host);
Log.d("chenzq1", "browser creating");
}
@Override
public int getMetricsCategory() {
return MetricsEvent.QS_BROWSER;//返回frameworks\base\proto\src\metrics_constants.proto中新增的定义
}
@Override
public CharSequence getTileLabel() {
return mContext.getString(R.string.quick_settings_ringer_browser);//返回Strings.xml定义的文件名
}
@Override
public BooleanState newTileState() {
return new BooleanState();
}
//长按快捷图标跳转计算器
@Override
public Intent getLongClickIntent() {
Log.d("chenzq2", "browser creating3");
Intent intent =new Intent();
intent.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
return new Intent(intent);
}
//单击快捷图标跳转浏览器
@Override
protected void handleClick() {
Log.d("chenzq2", "browser creating4");
Intent intent =new Intent();
intent.setClassName("org.chromium.webview_shell","org.chromium.webview_shell.WebViewBrowserActivity");
mContext.startActivity(intent);
}
//图标的构建
@Override
protected void handleUpdateState(BooleanState state, Object arg) {
Log.d("chenzq1", "browser creating2");
state.label = mContext.getString(R.string.quick_settings_ringer_browser);
state.icon = ResourceIcon.get(R.drawable.ic_qs_nfc_enabled);
state.state = Tile.STATE_ACTIVE;
}
@Override
public void handleSetListening(boolean listening) {
}
}