/
BrightnessUtils.java
115 lines (105 loc) · 3.6 KB
/
BrightnessUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.blankj.utilcode.util;
import android.content.ContentResolver;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/02/08
* desc : utils about brightness
* </pre>
*/
public final class BrightnessUtils {
private BrightnessUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether automatic brightness mode is enabled.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isAutoBrightnessEnabled() {
try {
int mode = Settings.System.getInt(
Utils.getApp().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE
);
return mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* Enable or disable automatic brightness mode.
* <p>Must hold {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
*
* @param enabled True to enabled, false otherwise.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean setAutoBrightnessEnabled(final boolean enabled) {
return Settings.System.putInt(
Utils.getApp().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
enabled ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
: Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
);
}
/**
* 获取屏幕亮度
*
* @return 屏幕亮度 0-255
*/
public static int getBrightness() {
try {
return Settings.System.getInt(
Utils.getApp().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS
);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return 0;
}
}
/**
* 设置屏幕亮度
* <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
* 并得到授权
*
* @param brightness 亮度值
*/
public static boolean setBrightness(@IntRange(from = 0, to = 255) final int brightness) {
ContentResolver resolver = Utils.getApp().getContentResolver();
boolean b = Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
resolver.notifyChange(Settings.System.getUriFor("screen_brightness"), null);
return b;
}
/**
* 设置窗口亮度
*
* @param window 窗口
* @param brightness 亮度值
*/
public static void setWindowBrightness(@NonNull final Window window,
@IntRange(from = 0, to = 255) final int brightness) {
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness / 255f;
window.setAttributes(lp);
}
/**
* 获取窗口亮度
*
* @param window 窗口
* @return 屏幕亮度 0-255
*/
public static int getWindowBrightness(@NonNull final Window window) {
WindowManager.LayoutParams lp = window.getAttributes();
float brightness = lp.screenBrightness;
if (brightness < 0) return getBrightness();
return (int) (brightness * 255);
}
}