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);
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理