forked from upstream-mirrors/quik
Fix logic for checking if it's night time
This commit is contained in:
@@ -24,8 +24,7 @@ import android.content.Intent
|
|||||||
import common.di.appComponent
|
import common.di.appComponent
|
||||||
import common.util.DateFormatter
|
import common.util.DateFormatter
|
||||||
import common.util.Preferences
|
import common.util.Preferences
|
||||||
import timber.log.Timber
|
import java.util.*
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class NightModeReceiver : BroadcastReceiver() {
|
class NightModeReceiver : BroadcastReceiver() {
|
||||||
@@ -38,19 +37,28 @@ class NightModeReceiver : BroadcastReceiver() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
Timber.v("onReceive")
|
|
||||||
|
|
||||||
// If night mode is not on auto, then there's nothing to do here
|
// If night mode is not on auto, then there's nothing to do here
|
||||||
if (prefs.nightMode.get() != Preferences.NIGHT_MODE_AUTO) {
|
if (prefs.nightMode.get() != Preferences.NIGHT_MODE_AUTO) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val dayMs = TimeUnit.DAYS.toMillis(1)
|
val nightStartTime = getPreviousInstanceOfTime(prefs.nightStart.get())
|
||||||
|
val nightEndTime = getPreviousInstanceOfTime(prefs.nightEnd.get())
|
||||||
|
|
||||||
val currentTime = (System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10)) % dayMs // add 10 mins in case receiver is called early
|
// If the last nightStart was more recent than the last nightEnd, then it's night time
|
||||||
val nightEndTime = dateFormatter.parseTime(prefs.nightEnd.get()).timeInMillis % dayMs
|
prefs.night.set(nightStartTime > nightEndTime)
|
||||||
val nightStartTime = dateFormatter.parseTime(prefs.nightStart.get()).timeInMillis % dayMs
|
}
|
||||||
|
|
||||||
prefs.night.set(currentTime in nightStartTime..nightEndTime)
|
/**
|
||||||
|
* Returns a Calendar set to the most recent occurrence of this time
|
||||||
|
*/
|
||||||
|
private fun getPreviousInstanceOfTime(time: String): Calendar {
|
||||||
|
val calendar = dateFormatter.parseTime(time)
|
||||||
|
|
||||||
|
while (calendar.timeInMillis > System.currentTimeMillis()) {
|
||||||
|
calendar.add(Calendar.DAY_OF_YEAR, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return calendar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user