Programme.kt 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package fr.forum_thalie.tsumugi.planning
  2. import android.util.Log
  3. import fr.forum_thalie.tsumugi.tag
  4. import java.util.*
  5. class Programme (val title: String, private val periodicity: Int, private val hourBegin: Int, private val hourEnd: Int) {
  6. fun isCurrent(): Boolean {
  7. val now = Calendar.getInstance()
  8. val currentDay =
  9. if (now.get(Calendar.DAY_OF_WEEK) - 1 == 0) 6 else now.get(Calendar.DAY_OF_WEEK) - 2
  10. // 0 (Monday) to 5 (Saturday) + 6 (Sunday)
  11. // this translates to "true" when:
  12. // - the currentDay is flagged in the "periodicity" bit array
  13. // OR
  14. // - Yesterday is flagged in the "periodicity" bit array AND the program does span over 2 days (night programs).
  15. // We'll check a after this whether the current hour is within the span.
  16. val isToday: Boolean = ((((1000000 shr currentDay) and (periodicity)) != 0))
  17. val isSpanningOverNight =
  18. (((1000000 shr ((currentDay - 1) % 7) and (periodicity)) != 0) && hourEnd < hourBegin)
  19. Log.d(tag, "$title is today: $isToday or spanning $isSpanningOverNight")
  20. // shr = shift-right. It's a binary mask.
  21. // if the program started yesterday, and spanned over night, it means that there could be a chance that it's still active.
  22. // we only need to check if the end time has been reached.
  23. if (isSpanningOverNight) {
  24. return (now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE) < hourEnd)
  25. }
  26. // if the program is today, we need to check if we're in the hour span.
  27. if (isToday) {
  28. val hasBegun =
  29. (now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE) >= hourBegin)
  30. val hasNotEnded =
  31. (now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE) < hourEnd) || hourEnd < hourBegin
  32. return (hasBegun && hasNotEnded)
  33. }
  34. return false
  35. }
  36. override fun toString(): String {
  37. return "Title: $title, time info (periodicity, begin, end): $periodicity, $hourBegin, $hourEnd"
  38. }
  39. init {
  40. Log.d(tag, this.toString())
  41. }
  42. }