BootBroadcastReceiver.kt 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package fr.riff_app.riff
  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 fr.riff_app.riff.alarm.RadioAlarm
  9. import fr.riff_app.riff.planning.Planning
  10. import fr.riff_app.riff.playerstore.PlayerStore
  11. class BootBroadcastReceiver : BroadcastReceiver(){
  12. override fun onReceive(context: Context, arg1: Intent) {
  13. //[REMOVE LOG CALLS]Log.d(tag, "Broadcast Receiver received $arg1")
  14. // define preferenceStore for places of the program that needs to access Preferences without a context
  15. preferenceStore = PreferenceManager.getDefaultSharedPreferences(context)
  16. if (arg1.action == Intent.ACTION_BOOT_COMPLETED) {
  17. RadioAlarm.instance.setNextAlarm(context) // schedule next alarm
  18. }
  19. if (arg1.getStringExtra("action") == "$tag.${Actions.PLAY_OR_FALLBACK.name}" )
  20. {
  21. RadioAlarm.instance.setNextAlarm(context) // schedule next alarm
  22. Planning.instance.parseUrl(context = context)
  23. if (!PlayerStore.instance.isInitialized)
  24. PlayerStore.instance.initApi()
  25. if (PlayerStore.instance.streamerName.value.isNullOrBlank())
  26. PlayerStore.instance.initPicture(context)
  27. val i = Intent(context, RadioService::class.java)
  28. i.putExtra("action", Actions.PLAY_OR_FALLBACK.name)
  29. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  30. context.startForegroundService(i)
  31. else
  32. context.startService(i)
  33. }
  34. }
  35. }