Reputation:
I've written code to create DialogPreference that should be working similar to default Android brightness setting dialog.
It works perfect as long as brightness mode is set to Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
. But when I change mode to Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
the phone brightness does not take the value of Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)
into account.
Let me show you an example:
Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS)
is still returning 50.Here is my code. I as said, it's in a class that extends DialogPreference, thus the onCreateDialogView method.
private static int MAX_BRIGHTNESS_VALUE = 255;
@Override
protected View onCreateDialogView() {
View dialogView = super.onCreateDialogView();
mAutomatic = (CheckBox) dialogView.findViewById(R.id.automatic_brightness);
mBrightnessBar = (SeekBar) dialogView.findViewById(R.id.brightness_bar);
mBrightnessBar.setMax(MAX_BRIGHTNESS_VALUE);
mAutomatic.setChecked(getBrightnessMode() == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
mBrightnessBar.setProgress(getBrightness());
mAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
setAutomaticBrightness();
} else {
setManualBrightness();
}
}
});
mBrightnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress > 10) {
setBrightness(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return dialogView;
}
private void setBrightness(int value) {
Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value);
// here I set current screen brightness programmatically
Window window = ((Activity) getContext()).getWindow();
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = value / (float) MAX_BRIGHTNESS_VALUE;
window.setAttributes(layoutpars);
}
private int getBrightness() {
try {
return Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
return MAX_BRIGHTNESS_VALUE;
}
}
private void setAutomaticBrightness() {
Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
private void setManualBrightness() {
Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
private int getBrightnessMode() {
try {
return Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
return Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
}
}
I have <uses-permission android:name="android.permission.WRITE_SETTINGS" />
in my AndroidManifest.xml
Does anyone know what is going on? I have seen some Stack Overflow threads with similar problems, but none of them got any helpful answer.
Upvotes: 3
Views: 2823
Reputation: 830
If you set brightness mode to automatic. then you need to stop automatic brightness adjustment before sets brightness. this code worked for me:
public static void stopAutoBrightness(Activity activity) {
android.provider.Settings.System.putInt(activity.getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
Upvotes: 1
Reputation:
It seems that it just works that way. If you set brightness mode to automatic by yourself, you cannot manipulate brightness level.
An example is here: BrightnessPreference code on grepcode.com for 4.1.2 In this Preference brightness SeekBar is disabled when brightness mode is automatic.
Based on BrightnessPreference code on grepcode.com for 4.4.2 I came up with the following inflexible solution:
@Override
protected void onClick() {
Intent intent = new Intent("com.android.settings.BrightnessPreferenceActivity");
startActivity(intent);
}
It just starts the Activity used in Android settings. It was good enough in my case.
Upvotes: 0