Add fallback to obtaining country
This commit is contained in:
@@ -30,7 +30,7 @@ object MacLookup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val macLookupBusy = mutableMapOf<Long, Pair<HttpURLConnection, Job>>()
|
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
|
@MainThread
|
||||||
fun abort(mac: Long) = macLookupBusy.remove(mac)?.let { (conn, job) ->
|
fun abort(mac: Long) = macLookupBusy.remove(mac)?.let { (conn, job) ->
|
||||||
@@ -48,9 +48,10 @@ object MacLookup {
|
|||||||
val obj = JSONObject(response).getJSONObject("result")
|
val obj = JSONObject(response).getJSONObject("result")
|
||||||
obj.optString("error", null)?.also { throw UnexpectedError(mac, it) }
|
obj.optString("error", null)?.also { throw UnexpectedError(mac, it) }
|
||||||
val company = obj.getString("company")
|
val company = obj.getString("company")
|
||||||
val country = obj.optString("country")
|
val match = obj.optString("country")?.let { countryCodeRegex.matchEntire(it) }
|
||||||
val result = if (countryCodeRegex.matchEntire(country) != null) {
|
?: obj.optString("address")?.let { countryCodeRegex.find(it) }
|
||||||
String(country.flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' + company
|
val result = if (match != null) {
|
||||||
|
String(match.groupValues[1].flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' + company
|
||||||
} else company.also { Timber.w(UnexpectedError(mac, response)) }
|
} else company.also { Timber.w(UnexpectedError(mac, response)) }
|
||||||
AppDatabase.instance.clientRecordDao.upsert(mac) {
|
AppDatabase.instance.clientRecordDao.upsert(mac) {
|
||||||
nickname = result
|
nickname = result
|
||||||
|
|||||||
Reference in New Issue
Block a user