package com.smarttpapers.reader.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.smarttpapers.reader.R;
public class DialogBrightness extends Dialog{
private int brightnessValue;
private boolean isAuto;
public DialogBrightness(final Context context, int theme) {
super(context, theme);
setContentView(R.layout.popup_brightness);
setTitle("Brightness");
setCanceledOnTouchOutside(true);
final SeekBar brightness = (SeekBar)findViewById(R.id.page_bightness);
int nowBrightnessValue = 0;
final ContentResolver resolver = ((Activity)context).getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(
resolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
brightness.setProgress(nowBrightnessValue);
isAuto=isAutoBrightness(resolver);
if(isAuto){
stopAutoBrightness((Activity)context);
}
brightness.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
saveBrightness(resolver, brightnessValue);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
brightnessValue=progress+10;
setBrightness((Activity)context, brightnessValue);
}
});
}
public boolean isAutoBrightness(ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return automicBrightness;
}
public void setBrightness(Activity activity, int bright) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = Float.valueOf(bright) * (1f / 255f);
activity.getWindow().setAttributes(lp);
}
public void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
public void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
public void saveBrightness(ContentResolver resolver, int bright) {
Uri uri = android.provider.Settings.System
.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(resolver, "screen_brightness",
bright);
resolver.notifyChange(uri, null);
}
}