1. 程式人生 > >Android O 下拉框增加自動亮度開關選項

Android O 下拉框增加自動亮度開關選項

本操作是更改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 Brightness</string>

圖片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,autobrightness</string>
<string name="quick_settings_tiles_stock" translatable="false">                       wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,work,cast,night,autobrightness</string>

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
    }

歡迎留言交流!