BootBroadcastReceiver.kt 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package io.r_a_d.radio2
  2. import android.content.BroadcastReceiver
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.os.Build
  6. import android.util.Log
  7. import androidx.preference.PreferenceManager
  8. import io.r_a_d.radio2.alarm.RadioAlarm
  9. import io.r_a_d.radio2.playerstore.PlayerStore
  10. import io.r_a_d.radio2.streamerNotificationService.WorkerStore
  11. import io.r_a_d.radio2.streamerNotificationService.startStreamerMonitor
  12. class BootBroadcastReceiver : BroadcastReceiver(){
  13. override fun onReceive(context: Context, arg1: Intent) {
  14. Log.d(tag, "Broadcast Receiver received $arg1")
  15. // define preferenceStore for places of the program that needs to access Preferences without a context
  16. preferenceStore = PreferenceManager.getDefaultSharedPreferences(context)
  17. if (arg1.action == Intent.ACTION_BOOT_COMPLETED) {
  18. WorkerStore.instance.init(context)
  19. startStreamerMonitor(context) // will actually start it only if enabled in settings
  20. RadioAlarm.instance.setNextAlarm(context) // schedule next alarm
  21. }
  22. if (arg1.getStringExtra("action") == "io.r_a_d.radio2.${Actions.PLAY_OR_FALLBACK.name}" )
  23. {
  24. RadioAlarm.instance.setNextAlarm(context) // schedule next alarm
  25. if (PlayerStore.instance.streamerName.value.isNullOrBlank())
  26. PlayerStore.instance.initPicture(context)
  27. if (!PlayerStore.instance.isInitialized)
  28. PlayerStore.instance.initApi()
  29. val i = Intent(context, RadioService::class.java)
  30. i.putExtra("action", Actions.PLAY_OR_FALLBACK.name)
  31. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  32. context.startForegroundService(i)
  33. else
  34. context.startService(i)
  35. }
  36. }
  37. }