ref: cc07ea7272bb1fe1445d9398145f46669a01013e
app/src/main/java/ml/adamsprogs/bimba/MessageReceiver.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package ml.adamsprogs.bimba import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.util.Log import ml.adamsprogs.bimba.models.Departure class MessageReceiver private constructor() : BroadcastReceiver() { companion object { private var receiver:MessageReceiver? = null fun getMessageReceiver(): MessageReceiver { if (receiver == null) receiver = MessageReceiver() return receiver as MessageReceiver } } private val onTimetableDownloadListeners: HashSet<OnTimetableDownloadListener> = HashSet() private val onVmListeners: HashSet<OnVmListener> = HashSet() override fun onReceive(context: Context?, intent: Intent?) { if (intent?.action == TimetableDownloader.ACTION_DOWNLOADED) { val result = intent.getStringExtra(TimetableDownloader.EXTRA_RESULT) for (listener in onTimetableDownloadListeners) { listener.onTimetableDownload(result) } } if (intent?.action == VmClient.ACTION_DEPARTURES_CREATED) { val departures = intent.getStringArrayListExtra(VmClient.EXTRA_DEPARTURES).map { Departure.fromString(it) } as ArrayList<Departure> val requester = intent.getStringExtra(VmClient.EXTRA_REQUESTER) val id = intent.getStringExtra(VmClient.EXTRA_ID) val size = intent.getIntExtra(VmClient.EXTRA_SIZE, -1) for (listener in onVmListeners) { listener.onVm(departures, requester, id, size) } } if (intent?.action == VmClient.ACTION_NO_DEPARTURES) { Log.i("VM", "Response received") val requester = intent.getStringExtra(VmClient.EXTRA_REQUESTER) val id = intent.getStringExtra(VmClient.EXTRA_ID) val size = intent.getIntExtra(VmClient.EXTRA_SIZE, -1) for (listener in onVmListeners) { listener.onVm(null, requester, id, size) } } } fun addOnTimetableDownloadListener(listener: OnTimetableDownloadListener) { onTimetableDownloadListeners.add(listener) } fun removeOnTimetableDownloadListener(listener: OnTimetableDownloadListener) { onTimetableDownloadListeners.remove(listener) } fun addOnVmListener(listener: OnVmListener) { onVmListeners.add(listener) } fun removeOnVmListener(listener: OnVmListener) { onVmListeners.remove(listener) } interface OnTimetableDownloadListener { fun onTimetableDownload(result: String?) } interface OnVmListener { fun onVm(vmDepartures: ArrayList<Departure>?, requester: String, id: String, size: Int) } } |