- 背光调节进度条源码分析
frameworks\base\packages\SystemUI\AndroidManifest.xml
<activity android:name=".settings.BrightnessDialog" android:label="@string/quick_settings_brightness_dialog_title" android:theme="@android:style/Theme.DeviceDefault.Dialog" android:finishOnCloseSystemDialogs="true" android:launchMode="singleInstance" android:excludeFromRecents="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessDialog.java
/** A dialog that provides controls for adjusting the screen brightness. */public class BrightnessDialog extends Activity {
private BrightnessController mBrightnessController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ImageView icon = (ImageView) findViewById(R.id.brightness_icon_on);
final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
mBrightnessController = new BrightnessController(this, icon, slider);
}
@Override
protected void onStart() {
super.onStart();
mBrightnessController.registerCallbacks();
MetricsLogger.visible(this, MetricsEvent.BRIGHTNESS_DIALOG);
}
@Override
protected void onStop() {
super.onStop();
MetricsLogger.hidden(this, MetricsEvent.BRIGHTNESS_DIALOG);
mBrightnessController.unregisterCallbacks();
}
}
BrightnessController 实现了ToggleSlider监听的类,其内部对象类是赋值传入的slider,从而与ToggleSlider 关联起来
mBrightnessController.registerCallbacks(); 将BrightnessController 设置为slider 的监听器,同时设置对数据库的监BrightnessController
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessController.java
public class BrightnessController implements ToggleSlider.Listener {
public interface BrightnessStateChangeCallback {
public void onBrightnessLevelChanged();
}
/** ContentObserver to watch brightness **/private class BrightnessObserver extends ContentObserver {
private final Uri BRIGHTNESS_MODE_URI =
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
private final Uri BRIGHTNESS_URI =
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
}
public void registerCallbacks() {
if (mListening) {
return;
}
mBrightnessObserver.startObserving();
mUserTracker.startTracking();
// Update the slider and mode before attaching the listener so we don't // receive the onChanged notifications for the initial values. updateMode();
updateSlider();
mControl.setOnChangedListener(this);
mListening = true;
}
}
当注册数据库监听器与ToggleSlider的监听器后,如果数据库改变,onChange 就会更新ToggleSlider ,如果ToggleSlider 改变了,这回调用其onChange函数,这个时候会更新数据库项,反过来由于数据库项变了,就会更新ToggleSlider 到对应的进度位置。
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\ToggleSlider.java
mToggle = (CompoundButton) findViewById(R.id.toggle);mToggle.setOnCheckedChangeListener(mCheckListener);
mSlider = (ToggleSeekBar) findViewById(R.id.slider);mSlider.setOnSeekBarChangeListener(mSeekListener);
private final OnCheckedChangeListener mCheckListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton toggle, boolean checked) {
mSlider.setEnabled(!checked);
if (mListener != null) {
mListener.onChanged(
ToggleSlider.this, mTracking, checked, mSlider.getProgress(), false);
}
if (mMirror != null) {
mMirror.mToggle.setChecked(checked);
}
}
};
private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mListener != null) {
mListener.onChanged(
ToggleSlider.this, mTracking, mToggle.isChecked(), progress, false);
}
}
ToggleSlider 是由多个控件组成的,这里一个是check控件、一个是进度条控件,并分别绑定对应的监听器。
private ToggleSlider mMirror;private BrightnessMirrorController mMirrorController;
ToggleSlider 这两个成员用于下拉设置中设置与隐藏亮度进度条。
public void setMirror(ToggleSlider toggleSlider) {
mMirror = toggleSlider;
if (mMirror != null) {
mMirror.setChecked(mToggle.isChecked());
mMirror.setMax(mSlider.getMax());
mMirror.setValue(mSlider.getProgress());
}
}
public void setMirrorController(BrightnessMirrorController c) {
mMirrorController = c;
}
这里将 mToggle、mSlider 值设置到 mMirror
frameworks\base\packages\SystemUI\src\com\android\systemui\qs\QSPanel.java
public void setBrightnessMirror(BrightnessMirrorController c) {
mBrightnessMirrorController = c;
ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
brightnessSlider.setMirror(mirror);
brightnessSlider.setMirrorController(c);
}
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java
mQSPanel.setBrightnessMirror(mBrightnessMirrorController);
这里 mBrightnessMirrorController控制 ToggleSlider mirror其在下拉菜单中的显示与隐藏,而其值由 mToggle、mSlider 的值设置。
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessController.java
public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value,
boolean stopTracking) {
updateIcon(mAutomatic);
if (mExternalChange) return;
if (!mAutomatic) {
setBrightness(val);
if (!tracking) {
AsyncTask.execute(new Runnable() {
public void run() {
Settings.System.putIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, val,
UserHandle.USER_CURRENT);
}
});
}
} else {
setBrightnessAdj(adj);
。。。
}
}
for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
cb.onBrightnessLevelChanged();
}
}
mAutomaticAvailable = context.getResources().getBoolean(
com.android.internal.R.bool.config_automatic_brightness_available);
自动亮度是否支持,设置Settings.System.SCREEN_BRIGHTNESS_MODE
public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";
支持则设置为Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC ,否则设置为 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
这里监听进度条改变,并记录到数据库中,并调用PMS设置亮度
private class BrightnessObserver extends ContentObserver {
private final Uri BRIGHTNESS_MODE_URI =
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
private final Uri BRIGHTNESS_URI =
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS)
BrightnessObserver(Handler handler) {
super(handler);
}
@Override public void onChange(boolean selfChange) {
onChange(selfChange, null);
}
@Override public void onChange(boolean selfChange, Uri uri) {
if (selfChange) return;
try {
mExternalChange = true;
if (BRIGHTNESS_MODE_URI.equals(uri)) {
updateMode();
updateSlider();
}
同时systemUI 中BrightnessController会注册监听器监听数据库改变从而改变进度条的位置