Add fallback to obtaining country

This commit is contained in:
Mygod
2019-01-31 17:31:10 +08:00
parent 509511461c
commit f75f539446

View File

@@ -30,7 +30,7 @@ object MacLookup {
}
private val macLookupBusy = mutableMapOf<Long, Pair<HttpURLConnection, Job>>()
private val countryCodeRegex = "[A-Z]{2}".toRegex() // http://en.wikipedia.org/wiki/ISO_3166-1
private val countryCodeRegex = "([A-Z]{2})\\s*\$".toRegex() // http://en.wikipedia.org/wiki/ISO_3166-1
@MainThread
fun abort(mac: Long) = macLookupBusy.remove(mac)?.let { (conn, job) ->
@@ -48,9 +48,10 @@ object MacLookup {
val obj = JSONObject(response).getJSONObject("result")
obj.optString("error", null)?.also { throw UnexpectedError(mac, it) }
val company = obj.getString("company")
val country = obj.optString("country")
val result = if (countryCodeRegex.matchEntire(country) != null) {
String(country.flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' + company
val match = obj.optString("country")?.let { countryCodeRegex.matchEntire(it) }
?: obj.optString("address")?.let { countryCodeRegex.find(it) }
val result = if (match != null) {
String(match.groupValues[1].flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' + company
} else company.also { Timber.w(UnexpectedError(mac, response)) }
AppDatabase.instance.clientRecordDao.upsert(mac) {
nickname = result