ref: 1c7e161e9bf542048dd7eb01e295eda6cfb2c536
app/src/main/java/ml/adamsprogs/bimba/datasources/VmService.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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
package ml.adamsprogs.bimba.datasources import android.app.Service import android.content.Intent import android.os.* import android.os.Process.THREAD_PRIORITY_BACKGROUND import com.google.gson.JsonObject import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.* import ml.adamsprogs.bimba.NetworkStateReceiver import ml.adamsprogs.bimba.calendarFromIso import ml.adamsprogs.bimba.models.* import ml.adamsprogs.bimba.secondsAfterMidnight import java.util.* import kotlin.collections.* class VmService : Service() { companion object { const val ACTION_READY = "ml.adamsprogs.bimba.action.vm.ready" const val EXTRA_DEPARTURES = "ml.adamsprogs.bimba.extra.vm.departures" const val EXTRA_PLATE_ID = "ml.adamsprogs.bimba.extra.vm.plate" const val EXTRA_STOP_CODE = "ml.adamsprogs.bimba.extra.vm.stop" const val TICK_6_ZINA_TIM = 12500L const val TICK_6_ZINA_TIM_WITH_MARGIN = TICK_6_ZINA_TIM * 3 / 4 } private var handler: Handler? = null private val tick6ZinaTim: Runnable = object : Runnable { override fun run() { handler!!.postDelayed(this, TICK_6_ZINA_TIM) try { for (plateId in requests.keys) launch(UI) { withContext(DefaultDispatcher) { downloadVM() } } } catch (e: IllegalArgumentException) { } } } private val requests = HashMap<String, Int>() private val vms = HashMap<String, Set<Plate>>() override fun onCreate() { val thread = HandlerThread("ServiceStartArguments", THREAD_PRIORITY_BACKGROUND) thread.start() handler = Handler(thread.looper) handler!!.postDelayed(tick6ZinaTim, TICK_6_ZINA_TIM) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val stopCode = intent?.getStringExtra("stop")!! val action = intent.action val once = intent.getBooleanExtra("once", false) if (action == "request") { if (isAlreadyRequested(stopCode)) { incrementRequest(stopCode) sendResult(stopCode) } else { if (!once) addRequest(stopCode) launch(UI) { withContext(DefaultDispatcher) { downloadVM(stopCode) } } } } else if (action == "remove") { decrementRequest(stopCode) cleanRequests() } return START_STICKY } private fun cleanRequests() { requests.forEach { if (it.value <= 0) requests.remove(it.key) } } private fun addRequest(stopCode: String) { if (requests[stopCode] == null) requests[stopCode] = 0 requests[stopCode] = requests[stopCode]!! + 1 } private fun incrementRequest(stopCode: String) { requests[stopCode] = requests[stopCode]!! + 1 } private fun decrementRequest(stopCode: String) { requests[stopCode] = requests[stopCode]!! - 1 } private fun isAlreadyRequested(stopCode: String): Boolean { return stopCode in requests } override fun onBind(intent: Intent): IBinder? { return null } override fun onDestroy() { } private suspend fun downloadVM() { vms.forEach { downloadVM(it.key) } } private suspend fun downloadVM(stopCode: String) { if (!NetworkStateReceiver.isNetworkAvailable(this)) { vms[stopCode] = emptySet() sendResult(stopCode, null, null) return } val javaRootMapObject = VmClient.getVmStopClient().makeRequest("getTimes", """{"symbol": "$stopCode"}""") if (!javaRootMapObject.has("success")) { sendResult(stopCode, null, null) return } val times = (javaRootMapObject["success"].asJsonObject)["times"].asJsonArray.map { it.asJsonObject } parseTimes(stopCode, times) } private fun parseTimes(stopCode: String, times: List<JsonObject>) { val date = Calendar.getInstance() val todayDay = "${date.get(Calendar.DATE)}".padStart(2, '0') val departures = HashMap<Plate.ID, HashSet<Departure>>() times.forEach { val thisLine = it["line"].asString val thisHeadsign = it["direction"].asString val thisPlateId = Plate.ID(thisLine, stopCode, thisHeadsign) if (departures[thisPlateId] == null) departures[thisPlateId] = HashSet() val departureDay = (it["departure"].asString).split("T")[0].split("-")[2] val departureTime = calendarFromIso(it["departure"].asString).secondsAfterMidnight() val departure = Departure(thisLine, listOf(-1), departureTime, false, ArrayList(), it["direction"].asString, it["realTime"].asBoolean, departureDay != todayDay, it["onStopPoint"].asBoolean) departures[thisPlateId]!!.add(departure) } departures.forEach { val departuresForPlate = HashMap<Int, HashSet<Departure>>() departuresForPlate[-1] = it.value val vm = HashSet<Plate>() vm.add(Plate(it.key, departuresForPlate)) vms[stopCode] = vm if (departures.isEmpty()) sendResult(stopCode, it.key, null) else sendResult(stopCode, it.key, it.value) } } private fun sendResult(stopCode: String) { vms[stopCode]?.forEach { sendResult(it.id.stop, it.id, it.departures?.get(-1)) } } private fun sendResult(stopCode: String, plateId: Plate.ID?, departures: HashSet<Departure>?) { val broadcastIntent = Intent() broadcastIntent.action = ACTION_READY broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT) if (departures != null) broadcastIntent.putStringArrayListExtra(EXTRA_DEPARTURES, departures.map { it.toString() } as ArrayList) broadcastIntent.putExtra(EXTRA_PLATE_ID, plateId) broadcastIntent.putExtra(EXTRA_STOP_CODE, stopCode) sendBroadcast(broadcastIntent) } } //note application stops the service on exit |