安卓的原生的SystemUI中下拉状态栏有一个亮度控制条,这个控制条可以控制手机的显示亮度,但是默认这里是没有自动亮度控制的开关的,本篇文章教大家如何在状态栏中添加一个自动亮度调节的开关,废话不多说,进入正题
首先我们来看一下下拉状态栏亮度进度条的位置,进度条的布局位于frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
它在frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java文件中被加载,所以我们要在quick_settings_brightness_dialog.xml中加一个控制自动亮度的CheckBox,位置可根据实际情况自己摆放
frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
>
<CheckBox
android:id="@+id/brightness_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@drawable/ic_checkbox_style"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:textSize="11dp"
android:layout_gravity="center_horizontal"
android:textColor="@color/gray_text_color"
android:text="@string/auto"
/>
</LinearLayout>
我在这里给这个CheckBox加了一个background,其实就是改变了CheckBox的样式,这个不难,网上有相关介绍,具体的样式定义如下
frameworks/base/packages/SystemUI/res/drawable/ic_check_box_normal.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#666666"
android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
</vector>
frameworks/base/packages/SystemUI/res/drawable/ic_check_box_select.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#56C0E5"
android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
</vector>
frameworks/base/packages/SystemUI/res/drawable/ic_checkbox_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_check_box_select" android:state_checked="true"/>
<item android:drawable="@drawable/ic_check_box_normal" android:state_checked="false"/>
</selector>
接着我们来看一下引用quick_settings_brightness_dialog的地方,frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java文件我们来看一下QSPanel的构造方法
public QSPanel(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setOrientation(VERTICAL);
mBrightnessView = LayoutInflater.from(context).inflate(
R.layout.quick_settings_brightness_dialog, this, false);
addView(mBrightnessView);
setupTileLayout();
mFooter = new QSFooter(this, context);
addView(mFooter.getView());
updateResources();
mBrightnessController = new BrightnessController(getContext(),
(ImageView) findViewById(R.id.brightness_icon),
(ToggleSlider) findViewById(R.id.brightness_slider));
}
这里我们可以看到通过LayoutInflater.from方法获取到了亮度进度条的View,然后添加到了QSPanel,QSPanel就是我们下拉状态栏看到的快捷设置面板,接下来我们可以看到创建了一个BrightnessController对象,对象的构造传入了(ImageView)findViewById(R.id.brightness_icon)和(ToggleSlider) findViewById(R.id.brightness_slider)这两个
控件对象,从这个对象的命名其实就可以看出来,亮度控制者,状态栏上亮度调节具体逻辑的实现全部是在这个类里面实现的,OK,我们可以在这里将CheckBox对象也传到这个类里面,代码如下
public QSPanel(Context context, AttributeSet attrs) {
super(context, attrs);
...........
...........
mBrightnessController = new BrightnessController(getContext(),
(ImageView) findViewById(R.id.brightness_icon),
(ToggleSlider) findViewById(R.id.brightness_slider));
mBrightnessController.setCheckBox((CheckBox)findViewById(R.id.brightness_check));
}
上述步骤完成后,我们就可以在BrightnessController这个类里面来具体实现自动亮度控制的逻辑
frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
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 void setCheckBox(CheckBox checkBox){
mCheckBox = checkBox;
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg1) {
Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
SCREEN_BRIGHTNESS_MODE_MANUAL);
}
Intent intent = new Intent();
intent.setAction("com.android.autobrightness");
mContext.sendBroadcast(intent);
}
});
}
通过改变SCREEN_BRIGHTNESS_MODE的值,便可以控制打开关闭自动亮度调节,此方法是我从Settings代码中找到的,亲试有效,这里发送了个广播是为了刷新设置的自动调节亮度界面显示问题,不用深究,这里还有一个问题,就是自动亮度调节的默认,或者在设置中改变了自动亮度调节按钮,怎么来刷新我们的CheckBox的显示,很好解决,BrightnessController这个类里面其实已经有了这个方法,叫做updateIcon方法,我们在这个类里可以直接设置CheckBox的显示
private void updateIcon(boolean automatic) {
if (mIcon != null) {
mIcon.setImageResource(automatic && SHOW_AUTOMATIC_ICON ?
com.android.systemui.R.drawable.ic_qs_brightness_auto_on :
com.android.systemui.R.drawable.ic_qs_brightness_auto_off);
}
if (mCheckBox != null) {
mCheckBox.setChecked(automatic);
}
}
这里还有最后一个要解决的问题,点击设置-显示-亮度,这里会弹出一个亮度控制条dialog,这个dialog其实也是quick_settings_brightness_dialog,所以我们需要把这个dialog中的CheckBox也传给BrightnessController,具体实现如下
frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...........
final ImageView icon = (ImageView) findViewById(R.id.brightness_icon);
final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
mBrightnessController = new BrightnessController(this, icon, slider);
CheckBox checkBox = (CheckBox) findViewById(R.id.brightness_check);
mBrightnessController.setCheckBox(checkBox);
}