0

I added SeekBar in Android app. I'm able to change the brightness of the system using SeekBar.

    binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                @RequiresApi(api = Build.VERSION_CODES.M)
                override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                    val permission = Settings.System.canWrite(applicationContext)
                    if (permission) {
                        var brightnessValue = p1 * 255 / 100
                        Settings.System.putInt(
                            applicationContext.contentResolver,
                            Settings.System.SCREEN_BRIGHTNESS_MODE,
                            Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
                        Settings.System.putInt(contentResolver, SCREEN_BRIGHTNESS, brightnessValue)
                        binding.seekBar.progress = brightnessValue
    
    
                    } else {
                        val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
                        intent.data = Uri.parse("package:" + applicationContext.packageName)
                        startActivityForResult(intent, 0)
                    }
                }
    
                override fun onStartTrackingTouch(p0: SeekBar?) {
                }
    
                override fun onStopTrackingTouch(p0: SeekBar?) {
    
                }
    
            })

This is how I can see the change in the system on the SeekBar.

    val contentObserver: ContentObserver = object : ContentObserver(Handler()) {
                override fun onChange(selfChange: Boolean) {
                    val a = Settings.System.getInt(contentResolver, SCREEN_BRIGHTNESS, 0)
                    Log.d("MainActivity", "Brightness value: $a")
                    binding.seekBar.progress = a* 255 / 100
                }
            }
            contentResolver.registerContentObserver(
                Settings.System.getUriFor(SCREEN_BRIGHTNESS),
                false, contentObserver
            )   

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.

When I use both together, the code doesn't work. How I can get it to work correctly both when I change it from the system and when I change it on the SeekBar?

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32

1 Answers1

0

I don't know what your actual problem is, but if they work separately but not together, you might have a feedback loop where a seekbar change causes a brightness change which causes a seekbar change which causes a brightness change...

If that's the case, you might want to look at your OnSeekbarChangeListener.onProgressChanged callback - you can't tell from the parameter names you have there, but the last parameter is a boolean that tells you whether the change was caused by a user interaction (i.e. moving the slider) or not (i.e. the value was set programmatically):

public abstract void onProgressChanged (SeekBar seekBar, 
                int progress, 
                boolean fromUser)

So you can check fromUser in onProgressChanged, and only set the system brightness if fromUser is true - otherwise your update is coming from the brightness change itself, and you don't want to set it again.

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • The problem is that Android devices have different screen brightness values, so it doesn't work consistently. – Halil Ozel Apr 17 '23 at 08:54
  • @HalilOzel `SCREEN_BRIGHTNESS` is always a value between *0* and *255*, it's just a range from min to max for that device. So long as you're reading and setting the values correctly, it should work consistently. It looks like you're converting to your progress bar range incorrectly - you're doing *(value / 100) x 255* in both directions, it should be *(value / 255) x 100* to convert from the brightness range to the progress bar range (if that range is *0-100*) – cactustictacs Apr 17 '23 at 18:11
  • 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. – Halil Ozel Apr 17 '23 at 18:27
  • @HalilOzel oh well if some OEMs are messing with that value you'll have to find a way to check for it I guess? I'd link you to a thread but your comment is already copied from it, either way that's a whole other topic! If this answer fixed your issue about not being able to do two-way communication with the progress bar, can you mark it as solved to help others? Thanks! – cactustictacs Apr 17 '23 at 18:36
  • No, this solution does not solve my problem. I already knew the other solution. – Halil Ozel Apr 17 '23 at 18:37
  • @HalilOzel well in that case, you might want to edit your question so it's specifically asking about inconsistent value ranges provided from different devices. You never actually said what your problem is beyond "the code doesn't work" and people can't help if they don't know what your issue is – cactustictacs Apr 17 '23 at 18:42
  • Sure. I added more details. – Halil Ozel Apr 17 '23 at 18:54