Programme.kt 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package fr.riff_app.riff.planning
  2. import android.util.Log
  3. import fr.riff_app.riff.tag
  4. import fr.riff_app.riff.weekdays
  5. import java.util.*
  6. import kotlin.collections.ArrayList
  7. class Programme (val title: String, private val periodicity: Int, private val hourBegin: Int, private val hourEnd: Int) {
  8. fun isThisDay(day: Int): Boolean {
  9. // 0 (Monday) to 5 (Saturday) + 6 (Sunday)
  10. // this translates to "true" when:
  11. // - the currentDay is flagged in the "periodicity" bit array
  12. // OR
  13. // - Yesterday is flagged in the "periodicity" bit array AND the program does span over 2 days (night programs).
  14. // We'll check a after this whether the current hour is within the span.
  15. return (((0b1000000 shr day) and (periodicity)) != 0)
  16. }
  17. fun isCurrent(): Boolean {
  18. val now = Calendar.getInstance(Planning.instance.timeZone)
  19. val currentDay =
  20. if (now.get(Calendar.DAY_OF_WEEK) - 1 == 0) 6 else now.get(Calendar.DAY_OF_WEEK) - 2
  21. // 0 (Monday) to 5 (Saturday) + 6 (Sunday)
  22. // this translates to "true" when:
  23. // - the currentDay is flagged in the "periodicity" bit array
  24. // OR
  25. // - Yesterday is flagged in the "periodicity" bit array AND the program does span over 2 days (night programs).
  26. // We'll check a after this whether the current hour is within the span.
  27. val isToday: Boolean = (((0b1000000 shr currentDay) and (periodicity)) != 0)
  28. val isSpanningOverNight =
  29. (((0b1000000 shr ((currentDay - 1) % 7) and (periodicity)) != 0) && hourEnd < hourBegin)
  30. //[REMOVE LOG CALLS]Log.d(tag, "$title is today: $isToday or spanning $isSpanningOverNight")
  31. // shr = shift-right. It's a binary mask.
  32. // if the program started yesterday, and spanned over night, it means that there could be a chance that it's still active.
  33. // we only need to check if the end time has been reached.
  34. if (isSpanningOverNight) {
  35. return (now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE) < hourEnd)
  36. }
  37. // if the program is today, we need to check if we're in the hour span.
  38. if (isToday) {
  39. val hasBegun =
  40. (now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE) >= hourBegin)
  41. val hasNotEnded =
  42. (now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE) < hourEnd) || hourEnd < hourBegin
  43. return (hasBegun && hasNotEnded)
  44. }
  45. return false
  46. }
  47. override fun toString(): String {
  48. return "Title: $title, time info (periodicity, begin, end): $periodicity, $hourBegin, $hourEnd"
  49. }
  50. private val offset1 = (Planning.instance.timeZone.getOffset(System.currentTimeMillis())) / (60*1000)
  51. private val offset2 = (Calendar.getInstance().timeZone.getOffset(System.currentTimeMillis())) / (60*1000)
  52. fun begin(): String {
  53. val hourBeginTZCorrect = hourBegin // + offset1 - offset2
  54. return "%02d:%02d".format(hourBeginTZCorrect/60, hourBeginTZCorrect%60)
  55. }
  56. fun end(): String {
  57. val hourEndTZCorrect = hourEnd // + offset1 - offset2
  58. return "%02d:%02d".format(hourEndTZCorrect/60, hourEndTZCorrect%60)
  59. }
  60. /*
  61. fun days(): String {
  62. val res = ArrayList<String>()
  63. for (i in 0 until weekdays.size) {
  64. if (((0b1000000 shr i) and (periodicity)) != 0)
  65. {
  66. res.add(weekdays[i])
  67. }
  68. }
  69. return res.toString().drop(1).dropLast(1) // drop '[' and ']'
  70. }
  71. */
  72. init {
  73. //[REMOVE LOG CALLS]Log.d(tag, this.toString())
  74. }
  75. }