Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect the screen brightness range on android?

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:

enter image description here

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?

like image 692
Grzegorz Adam Hankiewicz Avatar asked May 18 '19 23:05

Grzegorz Adam Hankiewicz


2 Answers

It is specific for some Xiaomi devices. For example Xiaomi Redmi Note 7 has 0-4000 range.

Official documentation defines SCREEN_BRIGHTNESS range as 0-255. So, I think there are no API to get the maximum value in brightness.

On some (not all) devices there is a file "/sys/class/leds/lcd-backlight/max_brightness" that can contain max value.

like image 101
Smak Avatar answered Oct 21 '22 13:10

Smak


Use this method to get max value for brightness

public int getMaxBrightness(Context context, int defaultValue){

    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if(powerManager != null) {
        Field[] fields = powerManager.getClass().getDeclaredFields();
        for (Field field: fields) {

            //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/PowerManager.java

            if(field.getName().equals("BRIGHTNESS_ON")) {
                field.setAccessible(true);
                try {
                    return (int) field.get(powerManager);
                } catch (IllegalAccessException e) {
                    return defaultValue;
                }
            }
        }
    }
    return defaultValue;
}

I have tested this on a couple of devices (mostly android 9, 10 devices, including some xiaomi devices, which usually set brightness higher than the usual 255), and looks like this is working.

Google does not promote accessing hidden fields/methods using reflection. Any android update could potentially break this solution.

Another possible solution would be accessing getMaximumScreenBrightnessSetting() method in PowerManager class using reflection. But I haven't tested it and therefore cannot confirm the results.

NB : If you are using this value to set brightness percentage, keep this in mind : From android 9 onwards, brightness is set logarithmically. So setting brightness percentage might not look like the percentage shown in brightness slider (Lower percentages might look higher than set value on brightness slider), but the physical brightness will be set right.

like image 35
Aswin P Ashok Avatar answered Oct 21 '22 14:10

Aswin P Ashok