1

I'm inheriting the "Theme.AppCompat.DayNight" theme to support dark mode automatically on Android Q and manually on previous versions. On Settings I let the user choose either On, Off or Automatic and then I use this code:

AppCompatDelegate.setDefaultNightMode(settings.darkMode())

Where settings.darkMode is:

        val darkValue =
            sharedPreferences.getString("PREF_DARK_THEME", null)
        return when (darkValue) {
            "ON" -> MODE_NIGHT_YES
            "OFF" -> MODE_NIGHT_NO
            else -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                MODE_NIGHT_FOLLOW_SYSTEM
            } else {
                MODE_NIGHT_AUTO_BATTERY
            }
        }

However I'd like to add support for Samsung One UI's "Night mode". Is it possible to read that system setting? I'd like to do something like this:

            ...
            else -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                MODE_NIGHT_FOLLOW_SYSTEM
            } else if("samsung"==Build.MANUFACTURER && isSamsungNightModeEnabled()) {
                MODE_NIGHT_YES
            } else ...[add other brands]...
            } else {
                MODE_NIGHT_AUTO_BATTERY
            }
Jorge Cevallos
  • 3,667
  • 3
  • 25
  • 37
  • 1
    Checking night mode flag in configuration works fine, I've tested it on S8. See https://stackoverflow.com/questions/44170028/android-how-to-detect-if-night-mode-is-on-when-using-appcompatdelegate-mode-ni/44170179#44170179 – Pawel Jan 29 '20 at 20:38
  • Thanks Pawel. I was under the impression context.resources.configuration.uiMode read only the local configuration (i.e. what was set with AppCompatDelegate.setDefaultNightMode) but fortunately it seems I was wrong. It seems to be doing what I wanted. – Jorge Cevallos Jan 29 '20 at 23:53

1 Answers1

0

Thanks to Pawel's comment here's what I've done. It seems to be working fine.

    fun darkMode(sharedPreferences: SharedPreferences, context: Context): Int {
        val darkValue =
            sharedPreferences.getString("PREF_DARK_THEME", null)
        return when (darkValue) {
            "ON" -> MODE_NIGHT_YES
            "OFF" -> MODE_NIGHT_NO
            else -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                MODE_NIGHT_FOLLOW_SYSTEM
            } else {
                val nightModeFlags =
                    context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
                when (nightModeFlags) {
                    Configuration.UI_MODE_NIGHT_YES -> MODE_NIGHT_YES
                    Configuration.UI_MODE_NIGHT_NO -> MODE_NIGHT_NO
                    else -> MODE_NIGHT_AUTO_BATTERY
                }
            }
        }
    }

I call AppCompatDelegate.setDefaultNightMode(darkMode(preferences, this)) from the activity's onResume() method and the application changes its theme automatically after modifying the device's "Night mode" on the notification panel. Tested on a Note 9.

Jorge Cevallos
  • 3,667
  • 3
  • 25
  • 37