StreamerMonitorService.kt 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package io.r_a_d.radio2.streamerNotificationService
  2. import android.app.Service
  3. import android.content.Intent
  4. import android.os.Build
  5. import android.os.IBinder
  6. import android.util.Log
  7. import androidx.core.app.NotificationCompat
  8. import androidx.lifecycle.Observer
  9. import androidx.preference.PreferenceManager
  10. import io.r_a_d.radio2.Actions
  11. import io.r_a_d.radio2.R
  12. import io.r_a_d.radio2.tag
  13. import java.util.*
  14. class StreamerMonitorService : Service() {
  15. override fun onBind(intent: Intent): IBinder? {
  16. return null // no binding allowed nor needed
  17. }
  18. private val streamerNameObserver: Observer<String> = Observer {
  19. val previousStreamer: String
  20. if (PreferenceManager.getDefaultSharedPreferences(this).contains("streamerName"))
  21. {
  22. previousStreamer = PreferenceManager.getDefaultSharedPreferences(this).getString("streamerName", "") ?: ""
  23. /* 3 conditions:
  24. - the streamer changed from previously
  25. - there is a previous non-empty streamer (at least second time running it)
  26. - the current streamer is non-empty (this can happen at Activity start where init() is called)
  27. */
  28. if (previousStreamer != it && previousStreamer != "" && it != "")
  29. {
  30. // notify
  31. val newStreamer = StreamerNotification(
  32. notificationChannelId = this.getString(R.string.streamerNotificationChannelId),
  33. notificationChannel = R.string.streamerNotificationChannel,
  34. notificationId = 3,
  35. notificationImportance = NotificationCompat.PRIORITY_DEFAULT
  36. )
  37. newStreamer.create(this)
  38. newStreamer.show()
  39. }
  40. }
  41. with(PreferenceManager.getDefaultSharedPreferences(this).edit()){
  42. putString("streamerName", it)
  43. commit()
  44. }
  45. }
  46. override fun onCreate() {
  47. super.onCreate()
  48. val streamerMonitorNotification = ServiceNotification(
  49. notificationChannelId = this.getString(R.string.streamerServiceChannelId),
  50. notificationChannel = R.string.streamerServiceChannel,
  51. notificationId = 2,
  52. notificationImportance = NotificationCompat.PRIORITY_LOW
  53. )
  54. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  55. {
  56. streamerMonitorNotification.create(this)
  57. streamerMonitorNotification.update()
  58. streamerMonitorNotification.show()
  59. startForeground(2, streamerMonitorNotification.notification)
  60. }
  61. WorkerStore.instance.tickerPeriod = 60 *
  62. (if (PreferenceManager.getDefaultSharedPreferences(this).contains("streamerMonitorPeriodPref"))
  63. Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString("streamerMonitorPeriodPref", "15")!!).toLong()
  64. else
  65. 15)
  66. Log.d(tag, "tickerPeriod = ${WorkerStore.instance.tickerPeriod}")
  67. with(PreferenceManager.getDefaultSharedPreferences(this).edit()){
  68. remove("streamerName")
  69. commit() // I commit on main thread to be sure it's been updated before continuing.
  70. }
  71. WorkerStore.instance.streamerName.observeForever(streamerNameObserver)
  72. WorkerStore.instance.isServiceStarted = true
  73. startNextAlarmStreamer(this)
  74. Log.d(tag, "streamerMonitor created")
  75. }
  76. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  77. val isNotifyingForNewStreamer = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("newStreamerNotification", false)
  78. // it's probably redundant but it shouldn't hurt
  79. if (!isNotifyingForNewStreamer || !WorkerStore.instance.isServiceStarted)
  80. {
  81. stopForeground(true)
  82. stopSelf()
  83. return START_NOT_STICKY
  84. }
  85. when (intent?.getStringExtra("action")) {
  86. Actions.NOTIFY.name -> {
  87. val date = Date() // given date
  88. val calendar = Calendar.getInstance() // creates a new calendar instance
  89. calendar.time = date // assigns calendar to given date
  90. val hours = calendar.get(Calendar.HOUR_OF_DAY) // gets hour in 24h format
  91. //val hours_american = calendar.get(Calendar.HOUR) // gets hour in 12h format
  92. val minutes = calendar.get(Calendar.MINUTE) // gets month number, NOTE this is zero based!
  93. Log.d(tag, "Fetched streamer name at ${hours}:${if (minutes < 10) "0" else ""}${minutes}")
  94. fetchStreamer(this)
  95. startNextAlarmStreamer(this) // schedule next alarm
  96. return START_STICKY
  97. }
  98. Actions.KILL.name -> {
  99. stopForeground(true)
  100. stopSelf()
  101. return START_NOT_STICKY
  102. }
  103. }
  104. return START_STICKY
  105. //super.onStartCommand(intent, flags, startId)
  106. }
  107. override fun onDestroy() {
  108. WorkerStore.instance.streamerName.removeObserver(streamerNameObserver)
  109. WorkerStore.instance.isServiceStarted = false
  110. super.onDestroy()
  111. }
  112. }