StreamerMonitorExtensions.kt 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package io.r_a_d.radio2.streamerNotificationService
  2. import android.app.AlarmManager
  3. import android.app.PendingIntent
  4. import android.content.BroadcastReceiver
  5. import android.content.Context
  6. import android.content.Intent
  7. import android.os.Build
  8. import android.os.SystemClock
  9. import android.util.Log
  10. import androidx.core.app.NotificationCompat
  11. import androidx.preference.PreferenceManager
  12. import io.r_a_d.radio2.*
  13. import io.r_a_d.radio2.alarm.RadioAlarm
  14. import io.r_a_d.radio2.playerstore.PlayerStore
  15. import org.json.JSONObject
  16. import java.net.URL
  17. fun startNextAlarmStreamer(c: Context){
  18. // the notification works with an alarm re-scheduled at fixed rate.
  19. // if the service stopped, the alarm is not re-scheduled.
  20. if (WorkerStore.instance.isServiceStarted)
  21. {
  22. val alarmIntent = Intent(c, StreamerMonitorService::class.java).let { intent ->
  23. intent.putExtra("action", Actions.NOTIFY.name)
  24. PendingIntent.getService(c, 0, intent, 0)
  25. }
  26. val alarmMgr = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
  27. when {
  28. Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> alarmMgr.setExactAndAllowWhileIdle(
  29. AlarmManager.ELAPSED_REALTIME_WAKEUP,
  30. SystemClock.elapsedRealtime() + WorkerStore.instance.tickerPeriod * 1000,
  31. alarmIntent
  32. )
  33. Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT -> alarmMgr.setExact(
  34. AlarmManager.ELAPSED_REALTIME_WAKEUP,
  35. SystemClock.elapsedRealtime() + WorkerStore.instance.tickerPeriod * 1000,
  36. alarmIntent
  37. )
  38. else -> alarmMgr.set(
  39. AlarmManager.ELAPSED_REALTIME_WAKEUP,
  40. SystemClock.elapsedRealtime() + WorkerStore.instance.tickerPeriod * 1000,
  41. alarmIntent
  42. )
  43. }
  44. } else {
  45. Log.d(tag, "alarm called while service is dead - skipped.")
  46. }
  47. }
  48. fun stopStreamerMonitor(context: Context)
  49. {
  50. val intent = Intent(context, StreamerMonitorService::class.java)
  51. intent.putExtra("action", Actions.KILL.name)
  52. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  53. context.startService(intent)
  54. } else {
  55. context.startService(intent)
  56. }
  57. Log.i(tag, "Service stopped")
  58. }
  59. fun startStreamerMonitor(context: Context, force: Boolean = false)
  60. {
  61. if (!force)
  62. {
  63. val isNotifyingForNewStreamer = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("newStreamerNotification", false)
  64. if (!isNotifyingForNewStreamer)
  65. return
  66. }
  67. val intent = Intent(context, StreamerMonitorService::class.java)
  68. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  69. context.startForegroundService(intent)
  70. } else {
  71. context.startService(intent)
  72. }
  73. Log.i(tag, "Service started on boot")
  74. }
  75. fun fetchStreamer(applicationContext: Context) {
  76. val urlToScrape = "https://r-a-d.io/api"
  77. val scrape : (Any?) -> String =
  78. {
  79. URL(urlToScrape).readText()
  80. }
  81. val post: (parameter: Any?) -> Unit = {
  82. val result = JSONObject(it as String)
  83. if (!result.isNull("main"))
  84. {
  85. val name = result.getJSONObject("main").getJSONObject("dj").getString("djname")
  86. WorkerStore.instance.streamerName.value = name
  87. }
  88. }
  89. // notify
  90. val t = ServiceNotification(
  91. notificationChannelId = applicationContext.getString(R.string.streamerServiceChannelId),
  92. notificationChannel = R.string.streamerServiceChannel,
  93. notificationId = 2,
  94. notificationImportance = NotificationCompat.PRIORITY_LOW
  95. )
  96. t.create(applicationContext)
  97. t.show()
  98. try{
  99. Async(scrape, post)
  100. Log.d(tag, "enqueue next work in ${WorkerStore.instance.tickerPeriod} seconds")
  101. } catch (e: Exception) {
  102. }
  103. }