StreamerNotifServiceFragment.kt 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package io.r_a_d.radio2.preferences
  2. import android.os.Bundle
  3. import androidx.appcompat.app.AlertDialog
  4. import androidx.preference.*
  5. import io.r_a_d.radio2.R
  6. import io.r_a_d.radio2.preferenceStore
  7. import io.r_a_d.radio2.streamerNotificationService.WorkerStore
  8. import io.r_a_d.radio2.streamerNotificationService.startStreamerMonitor
  9. import io.r_a_d.radio2.streamerNotificationService.stopStreamerMonitor
  10. class StreamerNotifServiceFragment : PreferenceFragmentCompat() {
  11. override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
  12. setPreferencesFromResource(R.xml.streamer_notif_service_preferences, rootKey)
  13. val streamerPeriod = preferenceScreen.findPreference<Preference>("streamerMonitorPeriodPref")
  14. val streamerNotification = preferenceScreen.findPreference<Preference>("newStreamerNotification")
  15. streamerNotification?.setOnPreferenceChangeListener { _, newValue ->
  16. if ((newValue as Boolean)) {
  17. val builder1 = AlertDialog.Builder(context!!)
  18. builder1.setMessage(R.string.warningStreamerNotif)
  19. builder1.setCancelable(false)
  20. builder1.setPositiveButton(
  21. "Yes"
  22. ) { dialog, _ ->
  23. startStreamerMonitor(context!!, force = true) // force enabled because the preference value is not yet set when running this callback.
  24. streamerPeriod?.isEnabled = true
  25. dialog.cancel()
  26. }
  27. builder1.setNegativeButton(
  28. "No"
  29. ) { dialog, _ ->
  30. stopStreamerMonitor(context!!)
  31. (streamerNotification as SwitchPreferenceCompat).isChecked = false
  32. dialog.cancel()
  33. }
  34. val alert11 = builder1.create()
  35. alert11.show()
  36. }
  37. else {
  38. stopStreamerMonitor(context!!)
  39. streamerPeriod?.isEnabled = false
  40. WorkerStore.instance.isServiceStarted = false
  41. }
  42. true
  43. }
  44. streamerPeriod?.summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
  45. streamerPeriod?.isEnabled = preferenceStore.getBoolean("newStreamerNotification", true)
  46. streamerPeriod?.setOnPreferenceChangeListener { _, newValue ->
  47. WorkerStore.instance.tickerPeriod = (Integer.parseInt(newValue as String)).toLong() * 60
  48. // this should be sufficient, the next alarm schedule should take the new tickerPeriod.
  49. true
  50. }
  51. }
  52. }