diff --git a/data/src/main/java/com/moez/QKSMS/util/NightModeManager.kt b/data/src/main/java/com/moez/QKSMS/util/NightModeManager.kt index 4ecaa1eb..243bfb53 100644 --- a/data/src/main/java/com/moez/QKSMS/util/NightModeManager.kt +++ b/data/src/main/java/com/moez/QKSMS/util/NightModeManager.kt @@ -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() } diff --git a/data/src/main/java/com/moez/QKSMS/util/Preferences.kt b/data/src/main/java/com/moez/QKSMS/util/Preferences.kt index 53437b2d..68a0d42d 100644 --- a/data/src/main/java/com/moez/QKSMS/util/Preferences.kt +++ b/data/src/main/java/com/moez/QKSMS/util/Preferences.kt @@ -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 { val default = rxPrefs.getInteger("theme", 0xFF0097A7.toInt()) diff --git a/presentation/src/main/AndroidManifest.xml b/presentation/src/main/AndroidManifest.xml index 8f164b00..7b183e5e 100644 --- a/presentation/src/main/AndroidManifest.xml +++ b/presentation/src/main/AndroidManifest.xml @@ -41,7 +41,7 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" - android:theme="@style/AppThemeDark"> + android:theme="@style/AppLaunchTheme"> + android:theme="@style/AppTheme" /> @@ -104,7 +104,7 @@ android:name=".feature.qkreply.QkReplyActivity" android:excludeFromRecents="true" android:taskAffinity="" - android:theme="@style/AppThemeLightDialog" + android:theme="@style/AppThemeDialog" android:windowSoftInputMode="adjustResize" /> diff --git a/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt b/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt index 5a4a2ad2..da9890fe 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/base/QkThemedActivity.kt @@ -23,7 +23,6 @@ import android.app.ActivityManager import android.graphics.BitmapFactory import android.os.Build import android.os.Bundle -import android.view.View import androidx.core.view.iterator import androidx.lifecycle.Lifecycle import com.moez.QKSMS.R @@ -33,10 +32,12 @@ import com.moez.QKSMS.util.Preferences import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.autoDisposable import io.reactivex.Observable +import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.rxkotlin.Observables import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.Subject import kotlinx.android.synthetic.main.toolbar.* +import java.util.concurrent.TimeUnit import javax.inject.Inject /** @@ -65,29 +66,17 @@ abstract class QkThemedActivity : QkActivity() { @SuppressLint("InlinedApi") override fun onCreate(savedInstanceState: Bundle?) { - - val night = prefs.night.get() - val black = prefs.black.get() - setTheme(getActivityThemeRes(night, black)) - + setTheme(getActivityThemeRes(prefs.black.get())) super.onCreate(savedInstanceState) // When certain preferences change, we need to recreate the activity - Observable.merge( - listOf(prefs.night, prefs.black, prefs.textSize, prefs.systemFont).map { it.asObservable().skip(1) }) + val triggers = listOf(prefs.nightMode, prefs.night, prefs.black, prefs.textSize, prefs.systemFont) + Observable.merge(triggers.map { it.asObservable().skip(1) }) + .debounce(400, TimeUnit.MILLISECONDS) + .observeOn(AndroidSchedulers.mainThread()) .autoDisposable(scope()) .subscribe { recreate() } - // Set the color for the status bar icons - // If night mode, or no dark icons supported, use light icons - // If night mode and only dark status icons supported, use dark status icons - // If night mode and all dark icons supported, use all dark icons - window.decorView.systemUiVisibility = when { - night || Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> 0 - Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR - else -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR - } - // Some devices don't let you modify android.R.attr.navigationBarColor if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { window.navigationBarColor = resolveThemeColor(android.R.attr.windowBackground) @@ -127,10 +116,9 @@ abstract class QkThemedActivity : QkActivity() { /** * This can be overridden in case an activity does not want to use the default themes */ - open fun getActivityThemeRes(night: Boolean, black: Boolean) = when { - night && black -> R.style.AppThemeBlack - night && !black -> R.style.AppThemeDark - else -> R.style.AppThemeLight + open fun getActivityThemeRes(black: Boolean) = when { + black -> R.style.AppTheme_Black + else -> R.style.AppTheme } } \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/common/widget/QkSwitch.kt b/presentation/src/main/java/com/moez/QKSMS/common/widget/QkSwitch.kt index e58a7bf0..500377d8 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/widget/QkSwitch.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/widget/QkSwitch.kt @@ -24,7 +24,7 @@ import android.util.AttributeSet import androidx.appcompat.widget.SwitchCompat import com.moez.QKSMS.R import com.moez.QKSMS.common.util.Colors -import com.moez.QKSMS.common.util.extensions.getColorCompat +import com.moez.QKSMS.common.util.extensions.resolveThemeColor import com.moez.QKSMS.common.util.extensions.withAlpha import com.moez.QKSMS.injection.appComponent import com.moez.QKSMS.util.Preferences @@ -50,26 +50,15 @@ class QkSwitch @JvmOverloads constructor(context: Context, attrs: AttributeSet? intArrayOf(android.R.attr.state_checked), intArrayOf()) - val themeColor = colors.theme().theme + thumbTintList = ColorStateList(states, intArrayOf( + context.resolveThemeColor(R.attr.switchThumbDisabled), + colors.theme().theme, + context.resolveThemeColor(R.attr.switchThumbEnabled))) - val switchThumbEnabled: Int = prefs.night.get() - .let { night -> if (night) R.color.switchThumbEnabledDark else R.color.switchThumbEnabledLight } - .let { res -> context.getColorCompat(res) } - - val switchThumbDisabled: Int = prefs.night.get() - .let { night -> if (night) R.color.switchThumbDisabledDark else R.color.switchThumbDisabledLight } - .let { res -> context.getColorCompat(res) } - - val switchTrackEnabled: Int = prefs.night.get() - .let { night -> if (night) R.color.switchTrackEnabledDark else R.color.switchTrackEnabledLight } - .let { res -> context.getColorCompat(res) } - - val switchTrackDisabled: Int = prefs.night.get() - .let { night -> if (night) R.color.switchTrackDisabledDark else R.color.switchTrackDisabledLight } - .let { res -> context.getColorCompat(res) } - - thumbTintList = ColorStateList(states, intArrayOf(switchThumbDisabled, themeColor, switchThumbEnabled)) - trackTintList = ColorStateList(states, intArrayOf(switchTrackDisabled, themeColor.withAlpha(0x4D), switchTrackEnabled)) + trackTintList = ColorStateList(states, intArrayOf( + context.resolveThemeColor(R.attr.switchTrackDisabled), + colors.theme().theme.withAlpha(0x4D), + context.resolveThemeColor(R.attr.switchTrackEnabled))) } } } \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyActivity.kt index f141efce..ead2fe26 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyActivity.kt @@ -123,10 +123,9 @@ class QkReplyActivity : QkThemedActivity(), QkReplyView { return true } - override fun getActivityThemeRes(night: Boolean, black: Boolean) = when { - night && black -> R.style.AppThemeBlackDialog - night && !black -> R.style.AppThemeDarkDialog - else -> R.style.AppThemeLightDialog + override fun getActivityThemeRes(black: Boolean) = when { + black -> R.style.AppThemeDialog_Black + else -> R.style.AppThemeDialog } } \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt index 0b19f744..8f8bd699 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsController.kt @@ -21,6 +21,7 @@ package com.moez.QKSMS.feature.settings import android.animation.ObjectAnimator import android.app.TimePickerDialog import android.content.Context +import android.os.Build import android.text.format.DateFormat import android.view.View import androidx.core.view.isVisible @@ -30,6 +31,7 @@ import com.jakewharton.rxbinding2.view.clicks import com.jakewharton.rxbinding2.view.longClicks import com.moez.QKSMS.BuildConfig import com.moez.QKSMS.R +import com.moez.QKSMS.common.MenuItem import com.moez.QKSMS.common.QkChangeHandler import com.moez.QKSMS.common.QkDialog import com.moez.QKSMS.common.base.QkController @@ -91,7 +93,12 @@ class SettingsController : QkController= 29) { + true -> nightModeDialog.adapter.setData(R.array.night_modes) + false -> nightModeDialog.adapter.data = context.resources.getStringArray(R.array.night_modes) + .mapIndexed { index, title -> MenuItem(title, index) } + .drop(1) + } textSizeDialog.adapter.setData(R.array.text_sizes) sendDelayDialog.adapter.setData(R.array.delayed_sending_labels) mmsSizeDialog.adapter.setData(R.array.mms_sizes, R.array.mms_sizes_ids) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsPresenter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsPresenter.kt index dd5d6bbd..eb25fc33 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsPresenter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/settings/SettingsPresenter.kt @@ -49,7 +49,9 @@ class SettingsPresenter @Inject constructor( private val nightModeManager: NightModeManager, private val prefs: Preferences, private val syncMessages: SyncMessages -) : QkPresenter(SettingsState()) { +) : QkPresenter(SettingsState( + nightModeId = prefs.nightMode.get() +)) { init { disposables += colors.themeObservable() diff --git a/presentation/src/main/res/values-night-v23/themes.xml b/presentation/src/main/res/values-night-v23/themes.xml new file mode 100644 index 00000000..3adbc7c0 --- /dev/null +++ b/presentation/src/main/res/values-night-v23/themes.xml @@ -0,0 +1,27 @@ + + + + + + + + diff --git a/presentation/src/main/res/values-night-v27/themes.xml b/presentation/src/main/res/values-night-v27/themes.xml new file mode 100644 index 00000000..b9c409bd --- /dev/null +++ b/presentation/src/main/res/values-night-v27/themes.xml @@ -0,0 +1,28 @@ + + + + + + + + diff --git a/presentation/src/main/res/values-night/themes.xml b/presentation/src/main/res/values-night/themes.xml new file mode 100644 index 00000000..97e52ecc --- /dev/null +++ b/presentation/src/main/res/values-night/themes.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + diff --git a/presentation/src/main/res/values-v23/themes.xml b/presentation/src/main/res/values-v23/themes.xml index 6ce2d43c..505f8f30 100644 --- a/presentation/src/main/res/values-v23/themes.xml +++ b/presentation/src/main/res/values-v23/themes.xml @@ -19,9 +19,9 @@ - - \ No newline at end of file + diff --git a/presentation/src/main/res/values-v27/themes.xml b/presentation/src/main/res/values-v27/themes.xml index 36808832..7ba93f2e 100644 --- a/presentation/src/main/res/values-v27/themes.xml +++ b/presentation/src/main/res/values-v27/themes.xml @@ -19,11 +19,11 @@ - - \ No newline at end of file + diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index 9af96628..1b28344d 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -343,6 +343,7 @@ The message to %s failed to send + System Disabled Always on Automatic diff --git a/presentation/src/main/res/values/themes.xml b/presentation/src/main/res/values/themes.xml index d897b24d..7a2a5a65 100644 --- a/presentation/src/main/res/values/themes.xml +++ b/presentation/src/main/res/values/themes.xml @@ -1,4 +1,5 @@ - - - - - - + + - - - - - - - - - - - \ No newline at end of file +