本操作是更改Android O 系统源码 MTK平台
首先来看看要实现的效果:
接下来看看具体实现步骤:
1 仿照
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/qs/tiles/ 目录下的java文件,自己新建个AutoBrightnessTile.java,放入其中:
package com.android.systemui.qs.tiles;
import android.util.Log;
import com.android.systemui.R;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import android.content.Intent;
import android.provider.Settings;
import android.service.quicksettings.Tile;
import android.widget.Switch;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.systemui.settings.BrightnessController;
import android.os.SystemProperties;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
public class AutoBrightnessTile extends QSTileImpl<BooleanState> {
private final Icon mIcon = ResourceIcon.get(R.drawable.ic_qs_brightness_auto_on);
private boolean isAuto;
public AutoBrightnessTile(QSHost host){
super(host);
}
@Override
public BooleanState newTileState() {
return new BooleanState();
}
@Override
protected void handleClick() {
isAuto = !mState.value;
Settings.System.putInt(mContext.getContentResolver(),
SCREEN_BRIGHTNESS_MODE,(isAuto ? 1 : 0) );//把设置的值写入
refreshState(isAuto);//更新
}
@Override
protected void handleUpdateState(BooleanState state, Object arg) {
if (state.slash == null) {
state.slash = new SlashState();
}
isAuto = Settings.System.getInt(mContext.getContentResolver(),
SCREEN_BRIGHTNESS_MODE, 0) == 1;//获取状态值
state.label = mContext.getString(R.string.quick_settings_auto_brightness);
state.icon = mIcon;//定义图标
state.value = isAuto;
state.slash.isSlashed = !state.value;
state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
}//设置状态切换
@Override
protected void handleUserSwitch(int newUserId) {
}
@Override
public Intent getLongClickIntent() {
return new Intent(Settings.ACTION_DISPLAY_SETTINGS);//长按监听
}
@Override
public void handleSetListening(boolean listening) {
}
@Override
public boolean isAvailable() {
return true;//可见
}
@Override
public CharSequence getTileLabel() {
return mContext.getString(R.string.quick_settings_auto_brightness);
}//定义标题
@Override
public int getMetricsCategory() {
return MetricsEvent.ACTION_BRIGHTNESS_AUTO;
}
}
2 定义自动亮度标题字符串和图标
vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/strings.xml
<string name="quick_settings_auto_brightness">Auto Brightnessstring>
图片ic_qs_brightness_auto_on在系统代码里就有了,不需要重新添加。
3 在 vendor/mediatek/proprietary/packages/apps/SystemUI/res/values/config.xml 中加入autobrightness相关属性
<string name="quick_settings_tiles_default" translatable="false"> wifi,bt,dnd,flashlight,rotation,battery,cell,airplane,cast,autobrightnessstring>
<string name="quick_settings_tiles_stock" translatable="false"> wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,work,cast,night,autobrightnessstring>
4 扫描是否有相关属性,有的话new这个AutoBrightnessTile类
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/qs/tileimpl/QSFactoryImpl.java
import com.android.systemui.qs.tiles.AutoBrightnessTile;
else if (tileSpec.equals("nfc")) return new NfcTile(mHost);
else if (tileSpec.equals("autobrightness")) return new AutoBrightnessTile(mHost);//joy
5 当点击 下拉框选项开关 ,Settings 相应设置也要改变,这里有两种方法,一种是广播监听。另一种是监听数据库变化,首先来看看第一种
vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/display/AutoBrightnessPreferenceController.java (注册并监听广播)
import android.content.BroadcastReceiver;//joy
import android.content.Intent;//joy
import android.content.IntentFilter;//joy
public class AutoBrightnessPreferenceController extends AbstractPreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private final static String Auto_Brightness_MODE = "mid.settings.autoBrightness";//joy
private SwitchPreference mPreference;//joy
public AutoBrightnessPreferenceController(Context context, String key) {
super(context);
mAutoBrightnessKey = key;
IntentFilter autoBrightnessFilter = new IntentFilter();//joy
autoBrightnessFilter.addAction(Auto_Brightness_MODE);//joy
context.registerReceiver(mAutoBrightnessBroadcastReceiver, autoBrightnessFilter);//joy
}
//joy add start
private void updateAutoState(){
int brightnessMode = Settings.System.getInt(mContext.getContentResolver(),
SYSTEM_KEY, DEFAULT_VALUE);
mPreference.setChecked(brightnessMode != DEFAULT_VALUE);
}
//add end
public void updateState(Preference preference) {
mPreference = (SwitchPreference) preference;//joy
updateAutoState();//joy
}
}
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/qs/tiles/AutoBrightnessTile.java (发送广播)
public class AutoBrightnessTile extends QSTileImpl<BooleanState> {
private final static String Auto_Brightness_MODE = "mid.settings.autoBrightness";//joy
protected void handleClick() {
isAuto = !mState.value;
Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,(isAuto ? 1 : 0) );
Intent intent = new Intent(Auto_Brightness_MODE);//joy
mContext.sendBroadcast(intent);//joy
refreshState(isAuto);
}
}
第二种在Settings监听数据库,相应数据有变化即时更新
vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/display/AutoBrightnessPreferenceController.java
import android.database.ContentObserver;//joy
import android.os.Handler;//joy
public class AutoBrightnessPreferenceController extends AbstractPreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private SettingsValueChangeContentObserver mContentOb;//joy
private SwitchPreference mPreference;//joy
public AutoBrightnessPreferenceController(Context context, String key) {
super(context);
mAutoBrightnessKey = key;
//joy add start
if(mContentOb==null){
mContentOb = new SettingsValueChangeContentObserver();
mContext.getContentResolver().registerContentObserver(Settings.System
.getUriFor(SCREEN_BRIGHTNESS_MODE),true, mContentOb);
}
//add end
}
//joy add start
class SettingsValueChangeContentObserver extends ContentObserver {
public SettingsValueChangeContentObserver() {
super( new Handler());
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
updateAutoState();
}
}
private void updateAutoState(){
int brightnessMode = Settings.System.getInt(mContext.getContentResolver(),
SYSTEM_KEY, DEFAULT_VALUE);
mPreference.setChecked(brightnessMode != DEFAULT_VALUE);
}
//add end
@Override
public void updateState(Preference preference) {
mPreference = (SwitchPreference) preference;//joy
updateAutoState();//joy
}
欢迎留言交流!