Files
e-Sun-Android/app/src/main/java/solutions/tretter/esunandroid/NightLight.kt
Tretzi e4610b3cd2 Rename app package to solutions.tretter.esunandroid
- Update Gradle namespace + applicationId
- Move Kotlin sources + tests to new package path
- Update package declarations across app
- Build debug APK v17
2026-05-04 20:37:14 -05:00

49 lines
1.7 KiB
Kotlin

package solutions.tretter.esunandroid
import android.content.Context
import android.content.Intent
import android.provider.Settings
/**
* Best-effort detection of Android's built-in Night Light ("Night display").
*
* Note: OEM-specific blue light filters may not be detectable.
*/
object NightLight {
fun isAndroidNightLightEnabled(context: Context): Boolean {
val cr = context.contentResolver
// AOSP Night Light
if (readSecureInt(cr, "night_display_activated") == 1) return true
// Samsung "Eye comfort shield" / blue light filter (best-effort; OEM keys may vary)
// Common keys observed on Samsung builds:
// - blue_light_filter (0/1)
// NOTE: We intentionally do NOT use *_opacity as a signal, because devices may keep the
// last-used opacity value even when the feature is turned off.
if (readSystemInt(cr, "blue_light_filter") == 1) return true
// Some builds use a different boolean key name.
if (readSystemInt(cr, "blue_light_filter_enabled") == 1) return true
return false
}
fun nightLightSettingsIntent(): Intent {
// Night Light settings screen (may not exist on all OEMs)
return Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS)
}
fun displaySettingsIntent(): Intent {
return Intent(Settings.ACTION_DISPLAY_SETTINGS)
}
private fun readSecureInt(cr: android.content.ContentResolver, key: String): Int? {
return runCatching { Settings.Secure.getInt(cr, key) }.getOrNull()
}
private fun readSystemInt(cr: android.content.ContentResolver, key: String): Int? {
return runCatching { Settings.System.getInt(cr, key) }.getOrNull()
}
}