Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3350b893 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

2790-P,Q-Dark-mode implementation

parent a9b71fbe
Loading
Loading
Loading
Loading
+4 −83
Original line number Diff line number Diff line
@@ -39,95 +39,16 @@ class NightModeManager @Inject constructor(
) {

    fun updateCurrentTheme() {
        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()
            }
        }
    }

    fun updateNightMode(mode: Int) {
        prefs.nightMode.set(mode)

        // 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
            })
        prefs.nightMode.set(Preferences.NIGHT_MODE_SYSTEM)
        prefs.night.set(false)
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
            widgetManager.updateTheme()
    }

        updateAlarms()
    }

    fun setNightStart(hour: Int, minute: Int) {
        prefs.nightStart.set("$hour:$minute")
        updateAlarms()
    }

    fun setNightEnd(hour: Int, minute: Int) {
        prefs.nightEnd.set("$hour:$minute")
        updateAlarms()
    }

    private fun updateAlarms() {
        val dayCalendar = createCalendar(prefs.nightEnd.get())
        val day = Intent(context, NightModeReceiver::class.java)
        val dayIntent = PendingIntent.getBroadcast(context, 0, day, 0)

        val nightCalendar = createCalendar(prefs.nightStart.get())
        val night = Intent(context, NightModeReceiver::class.java)
        val nightIntent = PendingIntent.getBroadcast(context, 1, night, 0)

        context.sendBroadcast(day)
        context.sendBroadcast(night)

        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        if (prefs.nightMode.get() == Preferences.NIGHT_MODE_AUTO) {
            alarmManager.setInexactRepeating(
                    AlarmManager.RTC_WAKEUP,
                    dayCalendar.timeInMillis,
                    AlarmManager.INTERVAL_DAY,
                    dayIntent
            )
            alarmManager.setInexactRepeating(
                    AlarmManager.RTC_WAKEUP,
                    nightCalendar.timeInMillis,
                    AlarmManager.INTERVAL_DAY,
                    nightIntent
            )
        } else {
            alarmManager.cancel(dayIntent)
            alarmManager.cancel(nightIntent)
        }
    }

    private fun createCalendar(time: String): Calendar {
        val calendar = parseTime(time)

+2 −10
Original line number Diff line number Diff line
@@ -82,10 +82,7 @@ class Preferences @Inject constructor(private val context: Context, private val
    val sia = rxPrefs.getBoolean("sia", false)

    // User configurable
    val nightMode = rxPrefs.getInteger("nightMode", when (Build.VERSION.SDK_INT >= 29) {
        true -> NIGHT_MODE_SYSTEM
        false -> NIGHT_MODE_OFF
    })
    val nightMode = rxPrefs.getInteger("nightMode", NIGHT_MODE_SYSTEM)
    val nightStart = rxPrefs.getString("nightStart", "18:00")
    val nightEnd = rxPrefs.getString("nightEnd", "6:00")
    val black = rxPrefs.getBoolean("black", false)
@@ -113,12 +110,7 @@ class Preferences @Inject constructor(private val context: Context, private val
        // 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
            })
            nightMode.set(NIGHT_MODE_SYSTEM)
            nightModeSummary.delete()
        }
    }
−8.93 KiB (113 KiB)

File changed.

No diff preview for this file type.

+2 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.moez.QKSMS.R
import com.moez.QKSMS.common.util.extensions.dpToPx
import com.moez.QKSMS.common.util.extensions.setPadding
import com.moez.QKSMS.injection.appComponent
@@ -46,7 +47,7 @@ class QkDialog @Inject constructor(private val context: Context, val adapter: Me
        recyclerView.adapter = adapter
        recyclerView.setPadding(top = 8.dpToPx(context), bottom = 8.dpToPx(context))

        val dialog = AlertDialog.Builder(activity)
        val dialog = AlertDialog.Builder(activity, R.style.customAlertDialog)
                .setTitle(title)
                .setView(recyclerView)
                .create()
+3 −16
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.iterator
import androidx.lifecycle.Lifecycle
import com.moez.QKSMS.R
@@ -63,7 +64,7 @@ abstract class QkThemedActivity : QkActivity() {

    @SuppressLint("InlinedApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(getActivityThemeRes(prefs.black.get()))
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState)

        // When certain preferences change, we need to recreate the activity
@@ -81,11 +82,6 @@ abstract class QkThemedActivity : QkActivity() {
                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)
        }

        // Set the color for the recent apps title
        val toolbarColor = resolveThemeColor(R.attr.colorPrimary)
        val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
@@ -97,20 +93,11 @@ abstract class QkThemedActivity : QkActivity() {
        super.onPostCreate(savedInstanceState)

        // Set the color for the overflow and navigation icon
        val textSecondary = resolveThemeColor(android.R.attr.textColorSecondary)
        val textSecondary = ContextCompat.getColor(this, R.color.colorAccent)
        toolbar?.overflowIcon = toolbar?.overflowIcon?.apply { setTint(textSecondary) }
    }

    open fun getColoredMenuItems(): List<Int> {
        return listOf()
    }

    /**
     * This can be overridden in case an activity does not want to use the default themes
     */
    open fun getActivityThemeRes(black: Boolean) = when {
        black -> R.style.AppTheme_Black
        else -> R.style.AppTheme
    }

}
 No newline at end of file
Loading