I'm using the following code to set the screen brightness, which works fine on most phones:
protected fun setBrightness(value: Float) {
//Set the system brightness using the brightness variable value
Settings.System.putInt(contentResolver, Settings.System
.SCREEN_BRIGHTNESS, (value * 255).toInt())
//Get the current window attributes
val layoutpars = window.getAttributes()
//Set the brightness of this window
layoutpars.screenBrightness = value
//Apply attribute changes to this window
window.setAttributes(layoutpars)
}
When I pass a value of 1, meaning maximum value, this gets converted to 255 which is said to be the greatest value to set the screen brightness. However, on a Xiaomi Mi8 setting the value to 255 won't set the brightness to the full range, as seen in this screenshot:
After printing some debug values and experimenting, it looks like on the Xiaomi Mi8 the maximum brightness value is actually 1024 (or at least, multiplying 1 by that value sets the full brightness bar).
It seems that different android devices might have different brightness scales. Is there some API to get the maximum value in brightness so I don't need to hardcode different constants?