#1425 - Add support for system dark mode on Android Q

This commit is contained in:
moezbhatti
2019-08-12 01:21:03 +02:00
parent c80985e59c
commit be20f19c82
15 changed files with 266 additions and 138 deletions
@@ -22,6 +22,7 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatDelegate
import com.moez.QKSMS.manager.WidgetManager
import com.moez.QKSMS.receiver.NightModeReceiver
import java.text.SimpleDateFormat
@@ -38,17 +39,33 @@ class NightModeManager @Inject constructor(
) {
fun updateCurrentTheme() {
// If night mode is not on auto, then there's nothing to do here
if (prefs.nightMode.get() != Preferences.NIGHT_MODE_AUTO) {
return
when (prefs.nightMode.get()) {
Preferences.NIGHT_MODE_SYSTEM -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
Preferences.NIGHT_MODE_OFF -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
Preferences.NIGHT_MODE_ON -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
Preferences.NIGHT_MODE_AUTO -> {
val nightStartTime = getPreviousInstanceOfTime(prefs.nightStart.get())
val nightEndTime = getPreviousInstanceOfTime(prefs.nightEnd.get())
// If the last nightStart was more recent than the last nightEnd, then it's night time
val night = nightStartTime > nightEndTime
prefs.night.set(night)
AppCompatDelegate.setDefaultNightMode(when (night) {
true -> AppCompatDelegate.MODE_NIGHT_YES
false -> AppCompatDelegate.MODE_NIGHT_NO
})
widgetManager.updateTheme()
}
}
val nightStartTime = getPreviousInstanceOfTime(prefs.nightStart.get())
val nightEndTime = getPreviousInstanceOfTime(prefs.nightEnd.get())
// If the last nightStart was more recent than the last nightEnd, then it's night time
prefs.night.set(nightStartTime > nightEndTime)
widgetManager.updateTheme()
}
fun updateNightMode(mode: Int) {
@@ -57,6 +74,12 @@ class NightModeManager @Inject constructor(
// If it's not on auto mode, set the appropriate night mode
if (mode != Preferences.NIGHT_MODE_AUTO) {
prefs.night.set(mode == Preferences.NIGHT_MODE_ON)
AppCompatDelegate.setDefaultNightMode(when (mode) {
Preferences.NIGHT_MODE_OFF -> AppCompatDelegate.MODE_NIGHT_NO
Preferences.NIGHT_MODE_ON -> AppCompatDelegate.MODE_NIGHT_YES
Preferences.NIGHT_MODE_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_NO
})
widgetManager.updateTheme()
}
@@ -29,9 +29,10 @@ import javax.inject.Singleton
class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences) {
companion object {
const val NIGHT_MODE_OFF = 0
const val NIGHT_MODE_ON = 1
const val NIGHT_MODE_AUTO = 2
const val NIGHT_MODE_SYSTEM = 0
const val NIGHT_MODE_OFF = 1
const val NIGHT_MODE_ON = 2
const val NIGHT_MODE_AUTO = 3
const val TEXT_SIZE_SMALL = 0
const val TEXT_SIZE_NORMAL = 1
@@ -66,7 +67,10 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
val canUseSubId = rxPrefs.getBoolean("canUseSubId", true)
// User configurable
val nightMode = rxPrefs.getInteger("nightModeSummary", NIGHT_MODE_OFF)
val nightMode = rxPrefs.getInteger("nightMode", when (Build.VERSION.SDK_INT >= 29) {
true -> NIGHT_MODE_SYSTEM
false -> NIGHT_MODE_OFF
})
val nightStart = rxPrefs.getString("nightStart", "18:00")
val nightEnd = rxPrefs.getString("nightEnd", "6:00")
val black = rxPrefs.getBoolean("black", false)
@@ -90,6 +94,20 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
val logging = rxPrefs.getBoolean("logging", false)
val version = rxPrefs.getInteger("version", 0)
init {
// Migrate from old night mode preference to new one, now that we support android Q night mode
val nightModeSummary = rxPrefs.getInteger("nightModeSummary")
if (nightModeSummary.isSet) {
nightMode.set(when (nightModeSummary.get()) {
0 -> NIGHT_MODE_OFF
1 -> NIGHT_MODE_ON
2 -> NIGHT_MODE_AUTO
else -> NIGHT_MODE_OFF
})
nightModeSummary.delete()
}
}
fun theme(threadId: Long = 0): Preference<Int> {
val default = rxPrefs.getInteger("theme", 0xFF0097A7.toInt())