Skip to content

Commit 09e6756

Browse files
Fiona Campbell3EleVen
authored andcommitted
Persist screen brightness setting through upgrade
The screen brightness float setting initially did not exist when upgrading the device software. This change ensures the float and int values are synchronised on the system start-up. Bug: 174508435 Test: manual - set autobrightness off, upgrade from Q to R, and check brightness value in settings. Change-Id: I2a3b996c8747e3c5f1d181bbdd438c70bf23d08b Merged-In: I2a3b996c8747e3c5f1d181bbdd438c70bf23d08b (cherry picked from commit 96f43f21b3a3021762c2d213d8958590127cae36) Signed-off-by: Akash Srivastava <akashniki@gmail.com>
1 parent fafdc8a commit 09e6756

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

core/java/com/android/internal/BrightnessSynchronizer.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* (new) system for storing the brightness. It has methods to convert between the two and also
3737
* observes for when one of the settings is changed and syncs this with the other.
3838
*/
39-
public class BrightnessSynchronizer{
39+
public class BrightnessSynchronizer {
4040

4141
private static final int MSG_UPDATE_FLOAT = 1;
4242
private static final int MSG_UPDATE_INT = 2;
@@ -78,6 +78,26 @@ public BrightnessSynchronizer(Context context) {
7878
mContext = context;
7979
mBrightnessSyncObserver = new BrightnessSyncObserver(mHandler);
8080
mBrightnessSyncObserver.startObserving();
81+
82+
// It is possible for the system to start up with the int and float values not
83+
// synchronized. So we force an update to the int value, since float is the source
84+
// of truth. Fallback to int value, if float is invalid. If both are invalid, use default
85+
// float value from config.
86+
final float currentFloatBrightness = getScreenBrightnessFloat(context);
87+
final int currentIntBrightness = getScreenBrightnessInt(context);
88+
89+
if (!Float.isNaN(currentFloatBrightness)) {
90+
updateBrightnessIntFromFloat(currentFloatBrightness);
91+
} else if (currentIntBrightness != -1) {
92+
updateBrightnessFloatFromInt(currentIntBrightness);
93+
} else {
94+
final float defaultBrightness = mContext.getResources().getFloat(
95+
com.android.internal.R.dimen.config_screenBrightnessSettingDefaultFloat);
96+
Settings.System.putFloatForUser(mContext.getContentResolver(),
97+
Settings.System.SCREEN_BRIGHTNESS_FLOAT, defaultBrightness,
98+
UserHandle.USER_CURRENT);
99+
100+
}
81101
}
82102

83103
/**
@@ -166,7 +186,8 @@ private static float getScreenBrightnessFloat(Context context) {
166186

167187
private static int getScreenBrightnessInt(Context context) {
168188
return Settings.System.getIntForUser(context.getContentResolver(),
169-
Settings.System.SCREEN_BRIGHTNESS, 0, UserHandle.USER_CURRENT);
189+
Settings.System.SCREEN_BRIGHTNESS, PowerManager.BRIGHTNESS_INVALID,
190+
UserHandle.USER_CURRENT);
170191
}
171192

172193
private float mPreferredSettingValue;

0 commit comments

Comments
 (0)