From f75f539446d175aec40f4011ae65b5dea6c9dc6f Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 31 Jan 2019 17:31:10 +0800 Subject: [PATCH] Add fallback to obtaining country --- .../main/java/be/mygod/vpnhotspot/client/MacLookup.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt index 1774dbeb..053e4df4 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt @@ -30,7 +30,7 @@ object MacLookup { } private val macLookupBusy = mutableMapOf>() - 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