Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / maps / src / main / java / com / vayunmathur / maps / data / GTFSProvider.kt
GTFSProvider.kt109 lines · 4.6 KiBRaw
| 1 | package com.vayunmathur.maps.data |
| 2 | import com.vayunmathur.maps.util.OfflineRouter |
| 3 | |
| 4 | import android.content.Context |
| 5 | import com.vayunmathur.library.log.Log |
| 6 | import java.util.concurrent.ConcurrentHashMap |
| 7 | |
| 8 | /** |
| 9 | * Last-resort route-colour lookup from a GTFS `routes.txt`. |
| 10 | * |
| 11 | * Pack-first: [OfflineRouterRouteBuilder] prefers the `route_color` baked into |
| 12 | * the transit pack and only falls back here. The lookup opens |
| 13 | * `assets/<feedName>/routes.txt` from the APK; no feed assets ship anymore |
| 14 | * (the SLO-only `US-CA-SLOT/` folder and the orphan `world_map.png` were |
| 15 | * deleted — the world pack covers San Francisco and the sidecars never did), |
| 16 | * so this returns null when absent rather than failing. API unchanged: the |
| 17 | * CSV parser is covered by `GTFSProviderTest`. |
| 18 | */ |
| 19 | object GTFSProvider { |
| 20 | // Accessed concurrently from OfflineRouter.getRoute (Dispatchers.Default) |
| 21 | // and from the map layers on the Main thread, so use a thread-safe map — |
| 22 | // a plain HashMap mutation race can corrupt internal buckets and cause |
| 23 | // ConcurrentModificationException or infinite get() loops. |
| 24 | private val routeColors = ConcurrentHashMap<String, String>() // Key: feedName:routeName, Value: #HEX |
| 25 | |
| 26 | fun getRouteColor(context: Context, feedName: String, routeName: String): String? { |
| 27 | val cacheKey = "$feedName:$routeName" |
| 28 | if (routeColors.containsKey(cacheKey)) return routeColors[cacheKey] |
| 29 | |
| 30 | return try { |
| 31 | findRouteColor(context, feedName, routeName)?.also { routeColors[cacheKey] = it } |
| 32 | } catch (e: java.io.IOException) { |
| 33 | Log.status("GTFSProvider", "Failed to read routes.txt for $feedName", e) |
| 34 | null |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private fun findRouteColor(context: Context, feedName: String, routeName: String): String? { |
| 39 | val assetPath = "$feedName/routes.txt" |
| 40 | context.assets.open(assetPath).use { inputStream -> |
| 41 | val reader = inputStream.bufferedReader() |
| 42 | val header = parseCsvLine(reader.readLine() ?: return null).map { it.trim() } |
| 43 | val columns = routeColumns(header) ?: return null |
| 44 | var line: String? |
| 45 | while (reader.readLine().also { line = it } != null) { |
| 46 | rowColor(parseCsvLine(line ?: continue), columns, routeName)?.let { return it } |
| 47 | } |
| 48 | } |
| 49 | return null |
| 50 | } |
| 51 | |
| 52 | private data class RouteColumns(val shortNameIdx: Int, val longNameIdx: Int, val colorIdx: Int) |
| 53 | |
| 54 | private fun routeColumns(header: List<String>): RouteColumns? { |
| 55 | val colorIdx = header.indexOf("route_color") |
| 56 | if (colorIdx == -1) return null |
| 57 | return RouteColumns( |
| 58 | shortNameIdx = header.indexOf("route_short_name"), |
| 59 | longNameIdx = header.indexOf("route_long_name"), |
| 60 | colorIdx = colorIdx, |
| 61 | ) |
| 62 | } |
| 63 | |
| 64 | private fun rowColor(parts: List<String>, columns: RouteColumns, routeName: String): String? { |
| 65 | val shortName = parts.getOrNull(columns.shortNameIdx) |
| 66 | val longName = parts.getOrNull(columns.longNameIdx) |
| 67 | if (shortName != routeName && longName != routeName) return null |
| 68 | return parts.getOrNull(columns.colorIdx)?.takeIf { it.isNotEmpty() }?.let { "#$it" } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Split one RFC 4180 CSV record into fields, honouring double-quoted fields |
| 73 | * that contain commas (GTFS routinely quotes `route_long_name`, e.g. |
| 74 | * `"Judah, Ocean Beach"`) and `""` escapes. A leading UTF-8 BOM is stripped. |
| 75 | * Records with embedded newlines are not supported — this is line-oriented, |
| 76 | * which is fine for the short rows in `routes.txt`. |
| 77 | */ |
| 78 | internal fun parseCsvLine(line: String): List<String> { |
| 79 | val fields = mutableListOf<String>() |
| 80 | val current = StringBuilder() |
| 81 | var inQuotes = false |
| 82 | var i = 0 |
| 83 | if (line.startsWith('\uFEFF')) i = 1 |
| 84 | while (i < line.length) { |
| 85 | val c = line[i] |
| 86 | when { |
| 87 | inQuotes -> |
| 88 | when { |
| 89 | c == '"' && i + 1 < line.length && line[i + 1] == '"' -> { |
| 90 | current.append('"') |
| 91 | i++ |
| 92 | } |
| 93 | c == '"' -> inQuotes = false |
| 94 | else -> current.append(c) |
| 95 | } |
| 96 | c == '"' -> inQuotes = true |
| 97 | c == ',' -> { |
| 98 | fields.add(current.toString()) |
| 99 | current.setLength(0) |
| 100 | } |
| 101 | c == '\r' -> {} // trailing CR from a CRLF file |
| 102 | else -> current.append(c) |
| 103 | } |
| 104 | i++ |
| 105 | } |
| 106 | fields.add(current.toString()) |
| 107 | return fields |
| 108 | } |
| 109 | } |